file_name
large_stringlengths
4
140
prefix
large_stringlengths
0
12.1k
suffix
large_stringlengths
0
12k
middle
large_stringlengths
0
7.51k
fim_type
large_stringclasses
4 values
utpgo.go
return c.baseConn.Close() }() // wait for socket to enter StateDestroying <-c.baseConnDestroyed c.setEncounteredError(net.ErrClosed) socketCloseErr := c.utpSocket.Close() // even if err was already set, this one is likely to be more helpful/interesting. if socketCloseErr != nil { err = socketCloseErr } return err } func (c *Conn) SetLogger(logger logr.Logger) { c.baseConn.SetLogger(logger) } func (c *Conn) Read(buf []byte) (n int, err error) { return c.ReadContext(context.Background(), buf) } func (c *Conn) stateEnterRead() error { switch { case c.readPending: return buffers.ReaderAlreadyWaitingErr case c.willClose: return c.makeOpError("read", net.ErrClosed) case c.remoteIsDone && c.readBuffer.SpaceUsed() == 0: return c.makeOpError("read", c.encounteredError) } c.readPending = true return nil } func (c *Conn) ReadContext(ctx context.Context, buf []byte) (n int, err error) { c.stateLock.Lock() encounteredErr := c.encounteredError deadline := c.readDeadline err = c.stateEnterRead() c.stateLock.Unlock() if err != nil { return 0, err } defer func() { c.stateLock.Lock() defer c.stateLock.Unlock() c.readPending = false }() if !deadline.IsZero() { var cancel func() ctx, cancel = context.WithDeadline(ctx, deadline) defer cancel() } for { var ok bool n, ok = c.readBuffer.TryConsume(buf) if ok { if n == 0 { return 0, io.EOF } return n, nil } if encounteredErr != nil { return 0, c.makeOpError("read", encounteredErr) } waitChan, cancelWait, err := c.readBuffer.WaitForBytesChan(1) if err != nil { return 0, err } select { case <-ctx.Done(): cancelWait() err = ctx.Err() if errors.Is(err, context.DeadlineExceeded) { // transform deadline error to os.ErrDeadlineExceeded as per // net.Conn specification err = c.makeOpError("read", os.ErrDeadlineExceeded) } return 0, err case <-c.closeChan: cancelWait() return 0, c.makeOpError("read", net.ErrClosed) case <-waitChan: } } } func (c *Conn) Write(buf []byte) (n int, err error) { return c.WriteContext(context.Background(), buf) } func (c *Conn) WriteContext(ctx context.Context, buf []byte) (n int, err error) { c.stateLock.Lock() if c.writePending { c.stateLock.Unlock() return 0, buffers.WriterAlreadyWaitingErr } c.writePending = true deadline := c.writeDeadline c.stateLock.Unlock() if err != nil { if err == io.EOF { // remote side closed connection cleanly, and µTP in/out streams // are not independently closeable. Doesn't make sense to return // an EOF from a Write method, so.. err = c.makeOpError("write", syscall.ECONNRESET) } else if err == net.ErrClosed { err = c.makeOpError("write", net.ErrClosed) } return 0, err } defer func() { c.stateLock.Lock() defer c.stateLock.Unlock() c.writePending = false }() if !deadline.IsZero() { var cancel func() ctx, cancel = context.WithDeadline(ctx, deadline) defer cancel() } for { c.stateLock.Lock() willClose := c.willClose remoteIsDone := c.remoteIsDone encounteredError := c.encounteredError c.stateLock.Unlock() if willClose { return 0, c.makeOpError("write", net.ErrClosed) } if remoteIsDone { return 0, c.makeOpError("write", encounteredError) } if ok := c.writeBuffer.TryAppend(buf); ok { // make sure µTP knows about the new bytes. this might be a bit // confusing, but it doesn't matter if other writes occur between // the TryAppend() above and the acquisition of the baseConnLock // below. All that matters is that (a) there is at least one call // to baseConn.Write scheduled to be made after this point (without // undue blocking); (b) baseConnLock is held when that Write call // is made; and (c) the amount of data in the write buffer does not // decrease between the SpaceUsed() call and the start of the next // call to onWriteCallback. func() { c.manager.baseConnLock.Lock() defer c.manager.baseConnLock.Unlock() amount := c.writeBuffer.SpaceUsed() c.logger.V(10).Info("informing libutp layer of data for writing", "len", amount) c.baseConn.Write(amount) }() return len(buf), nil } waitChan, cancelWait, err := c.writeBuffer.WaitForSpaceChan(len(buf)) if err != nil { if err == buffers.IsClosedErr { err = c.makeOpError("write", c.encounteredError) } return 0, err } // couldn't write the data yet; wait until we can, or until we hit the // timeout, or until the conn is closed. select { case <-ctx.Done(): cancelWait() err = ctx.Err() if errors.Is(err, context.DeadlineExceeded) { // transform deadline error to os.ErrDeadlineExceeded as per // net.Conn specification err = c.makeOpError("write", os.ErrDeadlineExceeded) } return 0, err case <-c.closeChan: cancelWait() return 0, c.makeOpError("write", net.ErrClosed) case <-waitChan: } } } func (c *Conn) RemoteAddr() net.Addr { // GetPeerName is thread-safe return (*Addr)(c.baseConn.GetPeerName()) } func (c *Conn) SetReadDeadline(t time.Time) error { c.stateLock.Lock() defer c.stateLock.Unlock() c.readDeadline = t return nil } func (c *Conn) SetWriteDeadline(t time.Time) error { c.stateLock.Lock() defer c.stateLock.Unlock() c.writeDeadline = t return nil } func (c *Conn) SetDeadline(t time.Time) error { c.stateLock.Lock() defer c.stateLock.Unlock() c.writeDeadline = t c.readDeadline = t return nil } func (c *Conn) makeOpError(op string, err error) error { opErr := c.utpSocket.makeOpError(op, err).(*net.OpError) opErr.Source = opErr.Addr opErr.Addr = c.RemoteAddr() return opErr } var _ net.Conn = &Conn{} func (l *Listener) AcceptUTPContext(ctx context.Context) (*Conn, error) { select { case newConn, ok := <-l.acceptChan: if ok { return newConn, nil } err := l.encounteredError if err == nil { err = l.makeOpError("accept", net.ErrClosed) } return nil, err case <-ctx.Done(): return nil, ctx.Err() } } func (l *Listener) AcceptUTP() (*Conn, error) { return l.AcceptUTPContext(context.Background()) } func (l *Listener) Accept() (net.Conn, error) { return l.AcceptUTP() } func (l *Listener) AcceptContext(ctx context.Context) (net.Conn, error) { return l.AcceptUTPContext(ctx) } func (l *Listener) Close() error { return l.utpSocket.Close() } func (l *Listener) Addr() net.Addr { return l.utpSocket.LocalAddr() } var _ net.Listener = &Listener{} func (u *utpSocket) makeOpError(op string, err error) error { return &net.OpError{ Op: op, Net: "utp", Source: nil, Addr: u.LocalAddr(), Err: err, } } func (u *utpSocket) Close() (err error) { u.stateLock.Lock() if u.manager != nil { err = u.manager.decrementReferences() u.manager = nil } u.stateLock.Unlock() return err } func (c *Conn) setEncounteredError(err error) { if err == nil { return } c.stateLock.Lock() defer c.stateLock.Unlock() // keep the first error if this is called multiple times if c.encounteredError == nil { c.encounteredError = err } if c.connecting { c.connecting = false
close(c.connectChan) } }
random_line_split
utpgo.go
error) { s := utpDialState{ logger: &noopLogger, } for _, opt := range options { opt.apply(&s) } switch network { case "utp", "utp4", "utp6": default: return nil, fmt.Errorf("network %s not supported", network) } udpAddr, err := ResolveUTPAddr(network, addr) if err != nil { return nil, err } listener, err := listen(s.logger, network, udpAddr) if err != nil { return nil, err } if s.tlsConfig != nil { return tls.NewListener(listener, s.tlsConfig), nil } return listener, nil } func ListenUTP(network string, localAddr *Addr) (*Listener, error) { return listen(&noopLogger, network, localAddr) } func ListenUTPOptions(network string, localAddr *Addr, options ...ConnectOption) (*Listener, error) { s := utpDialState{ logger: &noopLogger, } for _, opt := range options { opt.apply(&s) } return listen(s.logger, network, localAddr) } func listen(logger logr.Logger, network string, localAddr *Addr) (*Listener, error) { manager, err := newSocketManager(logger, network, (*net.UDPAddr)(localAddr), nil) if err != nil { return nil, err } udpLocalAddr := manager.LocalAddr().(*net.UDPAddr) utpListener := &Listener{ utpSocket: utpSocket{ localAddr: udpLocalAddr, manager: manager, }, acceptChan: manager.acceptChan, } manager.start() return utpListener, nil } type utpDialState struct { logger logr.Logger ctx context.Context tlsConfig *tls.Config } type ConnectOption interface { apply(s *utpDialState) } type optionLogger struct { logger logr.Logger } func (o *optionLogger) apply(s *utpDialState) { s.logger = o.logger } func WithLogger(logger logr.Logger) ConnectOption { return &optionLogger{logger: logger} } type optionContext struct { ctx context.Context } func (o *optionContext) apply(s *utpDialState) { s.ctx = o.ctx } func WithContext(ctx context.Context) ConnectOption { return &optionContext{ctx: ctx} } type optionTLS struct { tlsConfig *tls.Config } func (o *optionTLS) apply(s *utpDialState) { s.tlsConfig = o.tlsConfig } func WithTLS(tlsConfig *tls.Config) ConnectOption { return &optionTLS{tlsConfig: tlsConfig} } func (c *Conn) Close() error { // indicate our desire to close; once buffers are flushed, we can continue c.stateLock.Lock() if c.willClose { c.stateLock.Unlock() return errors.New("multiple calls to Close() not allowed") } c.willClose = true c.stateLock.Unlock() // wait for write buffer to be flushed c.writeBuffer.FlushAndClose() // if there are still any blocked reads, shut them down c.readBuffer.Close() // close baseConn err := func() error { // yes, even libutp.(*UTPSocket).Close() needs concurrency protection; // it may end up invoking callbacks c.manager.baseConnLock.Lock() defer c.manager.baseConnLock.Unlock() c.logger.V(10).Info("closing baseConn") c.libutpClosed = true return c.baseConn.Close() }() // wait for socket to enter StateDestroying <-c.baseConnDestroyed c.setEncounteredError(net.ErrClosed) socketCloseErr := c.utpSocket.Close() // even if err was already set, this one is likely to be more helpful/interesting. if socketCloseErr != nil { err = socketCloseErr } return err } func (c *Conn) SetLogger(logger logr.Logger) { c.baseConn.SetLogger(logger) } func (c *Conn) Read(buf []byte) (n int, err error) { return c.ReadContext(context.Background(), buf) } func (c *Conn) stateEnterRead() error { switch { case c.readPending: return buffers.ReaderAlreadyWaitingErr case c.willClose: return c.makeOpError("read", net.ErrClosed) case c.remoteIsDone && c.readBuffer.SpaceUsed() == 0: return c.makeOpError("read", c.encounteredError) } c.readPending = true return nil } func (c *Conn) ReadContext(ctx context.Context, buf []byte) (n int, err error) { c.stateLock.Lock() encounteredErr := c.encounteredError deadline := c.readDeadline err = c.stateEnterRead() c.stateLock.Unlock() if err != nil { return 0, err } defer func() { c.stateLock.Lock() defer c.stateLock.Unlock() c.readPending = false }() if !deadline.IsZero() { var cancel func() ctx, cancel = context.WithDeadline(ctx, deadline) defer cancel() } for { var ok bool n, ok = c.readBuffer.TryConsume(buf) if ok { if n == 0 { return 0, io.EOF } return n, nil } if encounteredErr != nil { return 0, c.makeOpError("read", encounteredErr) } waitChan, cancelWait, err := c.readBuffer.WaitForBytesChan(1) if err != nil { return 0, err } select { case <-ctx.Done(): cancelWait() err = ctx.Err() if errors.Is(err, context.DeadlineExceeded) { // transform deadline error to os.ErrDeadlineExceeded as per // net.Conn specification err = c.makeOpError("read", os.ErrDeadlineExceeded) } return 0, err case <-c.closeChan: cancelWait() return 0, c.makeOpError("read", net.ErrClosed) case <-waitChan: } } } func (c *Conn) Write(buf []byte) (n int, err error) {
func (c *Conn) WriteContext(ctx context.Context, buf []byte) (n int, err error) { c.stateLock.Lock() if c.writePending { c.stateLock.Unlock() return 0, buffers.WriterAlreadyWaitingErr } c.writePending = true deadline := c.writeDeadline c.stateLock.Unlock() if err != nil { if err == io.EOF { // remote side closed connection cleanly, and µTP in/out streams // are not independently closeable. Doesn't make sense to return // an EOF from a Write method, so.. err = c.makeOpError("write", syscall.ECONNRESET) } else if err == net.ErrClosed { err = c.makeOpError("write", net.ErrClosed) } return 0, err } defer func() { c.stateLock.Lock() defer c.stateLock.Unlock() c.writePending = false }() if !deadline.IsZero() { var cancel func() ctx, cancel = context.WithDeadline(ctx, deadline) defer cancel() } for { c.stateLock.Lock() willClose := c.willClose remoteIsDone := c.remoteIsDone encounteredError := c.encounteredError c.stateLock.Unlock() if willClose { return 0, c.makeOpError("write", net.ErrClosed) } if remoteIsDone { return 0, c.makeOpError("write", encounteredError) } if ok := c.writeBuffer.TryAppend(buf); ok { // make sure µTP knows about the new bytes. this might be a bit // confusing, but it doesn't matter if other writes occur between // the TryAppend() above and the acquisition of the baseConnLock // below. All that matters is that (a) there is at least one call // to baseConn.Write scheduled to be made after this point (without // undue blocking); (b) baseConnLock is held when that Write call // is made; and (c) the amount of data in the write buffer does not // decrease between the SpaceUsed() call and the start of the next // call to onWriteCallback. func() { c.manager.baseConnLock.Lock() defer c.manager.baseConnLock.Unlock() amount := c.writeBuffer.SpaceUsed() c.logger.V(10).Info("informing libutp layer of data for writing", "len", amount) c.baseConn.Write(amount) }() return len(buf), nil } waitChan, cancelWait, err := c.writeBuffer.WaitForSpaceChan(len(buf)) if err != nil { if err == buffers.IsClosedErr { err = c.makeOpError("write", c.encounteredError) } return 0, err } // couldn't
return c.WriteContext(context.Background(), buf) }
identifier_body
chain_spec.rs
NDb1JRwaHHVWyP9 hex!["f26cdb14b5aec7b2789fd5ca80f979cef3761897ae1f37ffb3e154cbcc1c2663"].into(), // 5EPQdAQ39WQNLCRjWsCk5jErsCitHiY5ZmjfWzzbXDoAoYbn hex!["66bc1e5d275da50b72b15de072a2468a5ad414919ca9054d2695767cf650012f"].into(), // 5DMa31Hd5u1dwoRKgC4uvqyrdK45RHv3CpwvpUC1EzuwDit4 hex!["3919132b851ef0fd2dae42a7e734fe547af5a6b809006100f48944d7fae8e8ef"].unchecked_into(), // 5C4vDQxA8LTck2xJEy4Yg1hM9qjDt4LvTQaMo4Y8ne43aU6x hex!["00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378"].unchecked_into(), // 5C4vDQxA8LTck2xJEy4Yg1hM9qjDt4LvTQaMo4Y8ne43aU6x hex!["00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378"].unchecked_into(), // 5C4vDQxA8LTck2xJEy4Yg1hM9qjDt4LvTQaMo4Y8ne43aU6x hex!["00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378"].unchecked_into(), ), ]; // generated with secret: subkey inspect "$secret"/fir let root_key: AccountId = hex![ // 5Ff3iXP75ruzroPWRP2FYBHWnmGGBSb63857BgnzCoXNxfPo "9ee5e5bdc0ec239eb164f865ecc345ce4c88e76ee002e0f7e318097347471809" ] .into(); let endowed_accounts: Vec<AccountId> = vec![root_key.clone()]; darwinia_genesis(initial_authorities, root_key, endowed_accounts, false, true) } let boot_nodes = vec![]; ChainSpec::from_genesis( "Staging Testnet", "staging_testnet", staging_testnet_config_genesis, boot_nodes, Some(TelemetryEndpoints::new(vec![(STAGING_TELEMETRY_URL.to_string(), 0)])), None, None, Default::default(), ) } /// Development config (single validator Alice) pub fn development_config() -> ChainSpec { fn development_config_genesis() -> GenesisConfig { darwinia_genesis( vec![get_authority_keys_from_seed("Alice")], get_account_id_from_seed::<sr25519::Public>("Alice"), vec![ get_account_id_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Bob"), get_account_id_from_seed::<sr25519::Public>("Charlie"), get_account_id_from_seed::<sr25519::Public>("Dave"), get_account_id_from_seed::<sr25519::Public>("Eve"), get_account_id_from_seed::<sr25519::Public>("Ferdie"), get_account_id_from_seed::<sr25519::Public>("Alice//stash"), get_account_id_from_seed::<sr25519::Public>("Bob//stash"), get_account_id_from_seed::<sr25519::Public>("Charlie//stash"), get_account_id_from_seed::<sr25519::Public>("Dave//stash"), get_account_id_from_seed::<sr25519::Public>("Eve//stash"), get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"), ], true, true, ) } ChainSpec::from_genesis( "Development", "dev", development_config_genesis, vec![], None, None, None, Default::default(), ) } /// IceFrog local testnet config (multivalidator Alice + Bob) pub fn local_testnet_config() -> ChainSpec
{ fn icefrog_config_genesis() -> GenesisConfig { darwinia_genesis( vec![ get_authority_keys_from_seed("Alice"), get_authority_keys_from_seed("Bob"), ], hex!["a60837b2782f7ffd23e95cd26d1aa8d493b8badc6636234ccd44db03c41fcc6c"].into(), // 5FpQFHfKd1xQ9HLZLQoG1JAQSCJoUEVBELnKsKNcuRLZejJR vec![ hex!["a60837b2782f7ffd23e95cd26d1aa8d493b8badc6636234ccd44db03c41fcc6c"].into(), hex!["f29311a581558ded67b8bfd097e614ce8135f777e29777d07ec501adb0ddab08"].into(), hex!["1098e3bf7b351d6210c61b05edefb3a2b88c9611db26fbed2c7136b6d8f9c90f"].into(), hex!["f252bc67e45acc9b3852a0ef84ddfce6c9cef25193617ef1421c460ecc2c746f"].into(), hex!["90ce56f84328b180fc55146709aa7038c18efd58f1f247410be0b1ddc612df27"].into(), hex!["4ca516c4b95488d0e6e9810a429a010b5716168d777c6b1399d3ed61cce1715c"].into(), hex!["e28573bb4d9233c799defe8f85fa80a66b43d47f4c1aef64bb8fffde1ecf8606"].into(), hex!["20e2455350cbe36631e82ce9b12152f98a3738cb763e46e65d1a253806a26d1a"].into(), hex!["9eccaca8a35f0659aed4df45455a855bcb3e7bff7bfc9d672b676bbb78988f0d"].into(), hex!["98dba2d3252825f4cd1141ca4f41ea201a22b4e129a6c7253cea546dbb20e442"].into(), ],
identifier_body
chain_spec.rs
; const RING_ENDOWMENT: Balance = 20_000_000 * COIN; const KTON_ENDOWMENT: Balance = 10 * COIN; const STASH: Balance = 1000 * COIN; GenesisConfig { frame_system: Some(SystemConfig { code: WASM_BINARY.to_vec(), changes_trie_config: Default::default(), }), pallet_indices: Some(IndicesConfig { ids: endowed_accounts .iter() .cloned() .chain(initial_authorities.iter().map(|x| x.0.clone())) .collect::<Vec<_>>(), }), pallet_session: Some(SessionConfig { keys: initial_authorities .iter() .map(|x| { ( x.0.clone(), session_keys(x.2.clone(), x.3.clone(), x.4.clone(), x.5.clone()), ) }) .collect::<Vec<_>>(), }), // pallet_democracy: Some(DemocracyConfig::default()), // pallet_collective_Instance1: Some(CouncilConfig { // members: endowed_accounts.iter().cloned().collect::<Vec<_>>()[..(num_endowed_accounts + 1) / 2].to_vec(), // phantom: Default::default(), // }), // pallet_collective_Instance2: Some(TechnicalCommitteeConfig { // members: endowed_accounts.iter().cloned().collect::<Vec<_>>()[..(num_endowed_accounts + 1) / 2].to_vec(), // phantom: Default::default(), // }), pallet_contracts: Some(ContractsConfig { current_schedule: pallet_contracts::Schedule { enable_println, // this should only be enabled on development chains ..Default::default() }, gas_price: 1 * MILLI, }), pallet_sudo: Some(SudoConfig { key: root_key }), pallet_babe: Some(BabeConfig { authorities: vec![] }), pallet_im_online: Some(ImOnlineConfig { keys: vec![] }), pallet_authority_discovery: Some(AuthorityDiscoveryConfig { keys: vec![] }), pallet_grandpa: Some(GrandpaConfig { authorities: vec![] }), // pallet_membership_Instance1: Some(Default::default()), // pallet_treasury: Some(Default::default()), pallet_ring: Some(BalancesConfig { balances: endowed_accounts .iter() .cloned() .map(|k| (k, RING_ENDOWMENT)) .chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH))) .collect(), vesting: vec![], }), pallet_kton: Some(KtonConfig { balances: endowed_accounts .iter() .cloned() .map(|k| (k, KTON_ENDOWMENT)) .chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH))) .collect(), vesting: vec![], }), pallet_staking: Some(StakingConfig { current_era: 0, validator_count: initial_authorities.len() as u32 * 2, minimum_validator_count: initial_authorities.len() as u32, stakers: initial_authorities .iter() .map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator)) .collect(), invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(), slash_reward_fraction: Perbill::from_percent(10), ..Default::default() }), } } /// Staging testnet config. pub fn staging_testnet_config() -> ChainSpec { fn staging_testnet_config_genesis() -> GenesisConfig { // stash, controller, session-key // generated with secret: // for i in 1 2 3 4 ; do for j in stash controller; do subkey inspect "$secret"/fir/$j/$i; done; done // and // for i in 1 2 3 4 ; do for j in session; do subkey --ed25519 inspect "$secret"//fir//$j//$i; done; done let initial_authorities: Vec<( AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, )> = vec![ ( // 5Fbsd6WXDGiLTxunqeK5BATNiocfCqu9bS1yArVjCgeBLkVy hex!["9c7a2ee14e565db0c69f78c7b4cd839fbf52b607d867e9e9c5a79042898a0d12"].into(), // 5EnCiV7wSHeNhjW3FSUwiJNkcc2SBkPLn5Nj93FmbLtBjQUq hex!["781ead1e2fa9ccb74b44c19d29cb2a7a4b5be3972927ae98cd3877523976a276"].into(), // 5Fb9ayurnxnaXj56CjmyQLBiadfRCqUbL2VWNbbe1nZU6wiC hex!["9becad03e6dcac03cee07edebca5475314861492cdfc96a2144a67bbe9699332"].unchecked_into(), // 5EZaeQ8djPcq9pheJUhgerXQZt9YaHnMJpiHMRhwQeinqUW8 hex!["6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"].unchecked_into(), // 5EZaeQ8djPcq9pheJUhgerXQZt9YaHnMJpiHMRhwQeinqUW8 hex!["6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"].unchecked_into(), // 5EZaeQ8djPcq9pheJUhgerXQZt9YaHnMJpiHMRhwQeinqUW8 hex!["6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"].unchecked_into(), ), ( // 5ERawXCzCWkjVq3xz1W5KGNtVx2VdefvZ62Bw1FEuZW4Vny2 hex!["68655684472b743e456907b398d3a44c113f189e56d1bbfd55e889e295dfde78"].into(), // 5Gc4vr42hH1uDZc93Nayk5G7i687bAQdHHc9unLuyeawHipF hex!["c8dc79e36b29395413399edaec3e20fcca7205fb19776ed8ddb25d6f427ec40e"].into(), // 5EockCXN6YkiNCDjpqqnbcqd4ad35nU4RmA1ikM4YeRN4WcE hex!["7932cff431e748892fa48e10c63c17d30f80ca42e4de3921e641249cd7fa3c2f"].unchecked_into(), // 5DhLtiaQd1L1LU9jaNeeu9HJkP6eyg3BwXA7iNM
{ vec![initial_authorities[0].clone().1, initial_authorities[1].clone().1] }
conditional_block
chain_spec.rs
(Default::default()), // pallet_treasury: Some(Default::default()), pallet_ring: Some(BalancesConfig { balances: endowed_accounts .iter() .cloned() .map(|k| (k, RING_ENDOWMENT)) .chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH))) .collect(), vesting: vec![], }), pallet_kton: Some(KtonConfig { balances: endowed_accounts .iter() .cloned() .map(|k| (k, KTON_ENDOWMENT)) .chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH))) .collect(), vesting: vec![], }),
stakers: initial_authorities .iter() .map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator)) .collect(), invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(), slash_reward_fraction: Perbill::from_percent(10), ..Default::default() }), } } /// Staging testnet config. pub fn staging_testnet_config() -> ChainSpec { fn staging_testnet_config_genesis() -> GenesisConfig { // stash, controller, session-key // generated with secret: // for i in 1 2 3 4 ; do for j in stash controller; do subkey inspect "$secret"/fir/$j/$i; done; done // and // for i in 1 2 3 4 ; do for j in session; do subkey --ed25519 inspect "$secret"//fir//$j//$i; done; done let initial_authorities: Vec<( AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, )> = vec![ ( // 5Fbsd6WXDGiLTxunqeK5BATNiocfCqu9bS1yArVjCgeBLkVy hex!["9c7a2ee14e565db0c69f78c7b4cd839fbf52b607d867e9e9c5a79042898a0d12"].into(), // 5EnCiV7wSHeNhjW3FSUwiJNkcc2SBkPLn5Nj93FmbLtBjQUq hex!["781ead1e2fa9ccb74b44c19d29cb2a7a4b5be3972927ae98cd3877523976a276"].into(), // 5Fb9ayurnxnaXj56CjmyQLBiadfRCqUbL2VWNbbe1nZU6wiC hex!["9becad03e6dcac03cee07edebca5475314861492cdfc96a2144a67bbe9699332"].unchecked_into(), // 5EZaeQ8djPcq9pheJUhgerXQZt9YaHnMJpiHMRhwQeinqUW8 hex!["6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"].unchecked_into(), // 5EZaeQ8djPcq9pheJUhgerXQZt9YaHnMJpiHMRhwQeinqUW8 hex!["6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"].unchecked_into(), // 5EZaeQ8djPcq9pheJUhgerXQZt9YaHnMJpiHMRhwQeinqUW8 hex!["6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"].unchecked_into(), ), ( // 5ERawXCzCWkjVq3xz1W5KGNtVx2VdefvZ62Bw1FEuZW4Vny2 hex!["68655684472b743e456907b398d3a44c113f189e56d1bbfd55e889e295dfde78"].into(), // 5Gc4vr42hH1uDZc93Nayk5G7i687bAQdHHc9unLuyeawHipF hex!["c8dc79e36b29395413399edaec3e20fcca7205fb19776ed8ddb25d6f427ec40e"].into(), // 5EockCXN6YkiNCDjpqqnbcqd4ad35nU4RmA1ikM4YeRN4WcE hex!["7932cff431e748892fa48e10c63c17d30f80ca42e4de3921e641249cd7fa3c2f"].unchecked_into(), // 5DhLtiaQd1L1LU9jaNeeu9HJkP6eyg3BwXA7iNMzKm7qqruQ hex!["482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e"].unchecked_into(), // 5DhLtiaQd1L1LU9jaNeeu9HJkP6eyg3BwXA7iNMzKm7qqruQ hex!["482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e"].unchecked_into(), // 5DhLtiaQd1L1LU9jaNeeu9HJkP6eyg3BwXA7iNMzKm7qqruQ hex!["482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e"].unchecked_into(), ), ( // 5DyVtKWPidondEu8iHZgi6Ffv9yrJJ1NDNLom3X9cTDi98qp hex!["547ff0ab649283a7ae01dbc2eb73932eba2fb09075e9485ff369082a2ff38d65"].into(), // 5FeD54vGVNpFX3PndHPXJ2MDakc462vBCD5mgtWRnWYCpZU9 hex!["9e42241d7cd91d001773b0b616d523dd80e13c6c2cab860b1234ef1b9ffc1526"].into(), // 5E1jLYfLdUQKrFrtqoKgFrRvxM3oQPMbf6DfcsrugZZ5Bn8d hex!["5633b70b80a6c8bb16270
pallet_staking: Some(StakingConfig { current_era: 0, validator_count: initial_authorities.len() as u32 * 2, minimum_validator_count: initial_authorities.len() as u32,
random_line_split
chain_spec.rs
1L1LU9jaNeeu9HJkP6eyg3BwXA7iNMzKm7qqruQ hex!["482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e"].unchecked_into(), ), ( // 5DyVtKWPidondEu8iHZgi6Ffv9yrJJ1NDNLom3X9cTDi98qp hex!["547ff0ab649283a7ae01dbc2eb73932eba2fb09075e9485ff369082a2ff38d65"].into(), // 5FeD54vGVNpFX3PndHPXJ2MDakc462vBCD5mgtWRnWYCpZU9 hex!["9e42241d7cd91d001773b0b616d523dd80e13c6c2cab860b1234ef1b9ffc1526"].into(), // 5E1jLYfLdUQKrFrtqoKgFrRvxM3oQPMbf6DfcsrugZZ5Bn8d hex!["5633b70b80a6c8bb16270f82cca6d56b27ed7b76c8fd5af2986a25a4788ce440"].unchecked_into(), // 5DhKqkHRkndJu8vq7pi2Q5S3DfftWJHGxbEUNH43b46qNspH hex!["482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a"].unchecked_into(), // 5DhKqkHRkndJu8vq7pi2Q5S3DfftWJHGxbEUNH43b46qNspH hex!["482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a"].unchecked_into(), // 5DhKqkHRkndJu8vq7pi2Q5S3DfftWJHGxbEUNH43b46qNspH hex!["482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a"].unchecked_into(), ), ( // 5HYZnKWe5FVZQ33ZRJK1rG3WaLMztxWrrNDb1JRwaHHVWyP9 hex!["f26cdb14b5aec7b2789fd5ca80f979cef3761897ae1f37ffb3e154cbcc1c2663"].into(), // 5EPQdAQ39WQNLCRjWsCk5jErsCitHiY5ZmjfWzzbXDoAoYbn hex!["66bc1e5d275da50b72b15de072a2468a5ad414919ca9054d2695767cf650012f"].into(), // 5DMa31Hd5u1dwoRKgC4uvqyrdK45RHv3CpwvpUC1EzuwDit4 hex!["3919132b851ef0fd2dae42a7e734fe547af5a6b809006100f48944d7fae8e8ef"].unchecked_into(), // 5C4vDQxA8LTck2xJEy4Yg1hM9qjDt4LvTQaMo4Y8ne43aU6x hex!["00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378"].unchecked_into(), // 5C4vDQxA8LTck2xJEy4Yg1hM9qjDt4LvTQaMo4Y8ne43aU6x hex!["00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378"].unchecked_into(), // 5C4vDQxA8LTck2xJEy4Yg1hM9qjDt4LvTQaMo4Y8ne43aU6x hex!["00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378"].unchecked_into(), ), ]; // generated with secret: subkey inspect "$secret"/fir let root_key: AccountId = hex![ // 5Ff3iXP75ruzroPWRP2FYBHWnmGGBSb63857BgnzCoXNxfPo "9ee5e5bdc0ec239eb164f865ecc345ce4c88e76ee002e0f7e318097347471809" ] .into(); let endowed_accounts: Vec<AccountId> = vec![root_key.clone()]; darwinia_genesis(initial_authorities, root_key, endowed_accounts, false, true) } let boot_nodes = vec![]; ChainSpec::from_genesis( "Staging Testnet", "staging_testnet", staging_testnet_config_genesis, boot_nodes, Some(TelemetryEndpoints::new(vec![(STAGING_TELEMETRY_URL.to_string(), 0)])), None, None, Default::default(), ) } /// Development config (single validator Alice) pub fn development_config() -> ChainSpec { fn development_config_genesis() -> GenesisConfig { darwinia_genesis( vec![get_authority_keys_from_seed("Alice")], get_account_id_from_seed::<sr25519::Public>("Alice"), vec![ get_account_id_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Bob"), get_account_id_from_seed::<sr25519::Public>("Charlie"), get_account_id_from_seed::<sr25519::Public>("Dave"), get_account_id_from_seed::<sr25519::Public>("Eve"), get_account_id_from_seed::<sr25519::Public>("Ferdie"), get_account_id_from_seed::<sr25519::Public>("Alice//stash"), get_account_id_from_seed::<sr25519::Public>("Bob//stash"), get_account_id_from_seed::<sr25519::Public>("Charlie//stash"), get_account_id_from_seed::<sr25519::Public>("Dave//stash"), get_account_id_from_seed::<sr25519::Public>("Eve//stash"), get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"), ], true, true, ) } ChainSpec::from_genesis( "Development", "dev", development_config_genesis, vec![], None, None, None, Default::default(), ) } /// IceFrog local testnet config (multivalidator Alice + Bob) pub fn local_testnet_config() -> ChainSpec { fn
icefrog_config_genesis
identifier_name
mnist_benchmark.py
us """ GCP_ENV = 'PATH=/tmp/pkb/google-cloud-sdk/bin:$PATH' flags.DEFINE_string('mnist_data_dir', None, 'mnist train file for tensorflow') flags.DEFINE_string('imagenet_data_dir', 'gs://cloud-tpu-test-datasets/fake_imagenet', 'Directory where the input data is stored') flags.DEFINE_string( 't2t_data_dir', None, 'Directory where the input data is stored for tensor2tensor') flags.DEFINE_integer('imagenet_num_train_images', 1281167, 'Size of ImageNet training data set.') flags.DEFINE_integer('imagenet_num_eval_images', 50000, 'Size of ImageNet validation data set.') flags.DEFINE_integer('mnist_num_train_images', 55000, 'Size of MNIST training data set.') flags.DEFINE_integer('mnist_num_eval_images', 5000, 'Size of MNIST validation data set.') flags.DEFINE_integer('mnist_train_epochs', 37, 'Total number of training echos', lower_bound=1) flags.DEFINE_integer( 'mnist_eval_epochs', 0, 'Total number of evaluation epochs. If `0`, evaluation ' 'after training is skipped.') flags.DEFINE_integer('tpu_iterations', 500, 'Number of iterations per TPU training loop.') flags.DEFINE_integer('mnist_batch_size', 1024, 'Mini-batch size for the training. Note that this ' 'is the global batch size and not the per-shard batch.') flags.DEFINE_enum('tpu_precision', 'bfloat16', ['bfloat16', 'float32'], 'Precision to use') EXAMPLES_PER_SECOND_PRECISION = 0.01 def GetConfig(user_config): """Load and return benchmark config. Args: user_config: user supplied configuration (flags and config file) Returns: loaded benchmark configuration """ return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME) def _UpdateBenchmarkSpecWithFlags(benchmark_spec): """Update the benchmark_spec with supplied command line flags. Args: benchmark_spec: benchmark specification to update """ benchmark_spec.data_dir = FLAGS.mnist_data_dir benchmark_spec.iterations = FLAGS.tpu_iterations benchmark_spec.gcp_service_account = FLAGS.gcp_service_account benchmark_spec.batch_size = FLAGS.mnist_batch_size benchmark_spec.num_train_images = FLAGS.mnist_num_train_images benchmark_spec.num_eval_images = FLAGS.mnist_num_eval_images benchmark_spec.num_examples_per_epoch = ( float(benchmark_spec.num_train_images) / benchmark_spec.batch_size) benchmark_spec.train_epochs = FLAGS.mnist_train_epochs benchmark_spec.train_steps = int( benchmark_spec.train_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.eval_epochs = FLAGS.mnist_eval_epochs benchmark_spec.eval_steps = int( benchmark_spec.eval_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.precision = FLAGS.tpu_precision benchmark_spec.env_cmd = 'export PYTHONPATH=$PYTHONPATH:$PWD/tpu/models' def Prepare(benchmark_spec): """Install and set up MNIST on the target vm. Args: benchmark_spec: The benchmark specification """ benchmark_spec.always_call_cleanup = True _UpdateBenchmarkSpecWithFlags(benchmark_spec) vm = benchmark_spec.vms[0] if not benchmark_spec.tpus: vm.Install('tensorflow') vm.Install('cloud_tpu_models') vm.Install('tensorflow_models') if benchmark_spec.tpus: storage_service = gcs.GoogleCloudStorageService() benchmark_spec.storage_service = storage_service bucket = 'pkb{}'.format(FLAGS.run_uri) benchmark_spec.bucket = bucket benchmark_spec.model_dir = 'gs://{}'.format(bucket) location = benchmark_spec.tpu_groups['train'].GetZone() storage_service.PrepareService(util.GetRegionFromZone(location)) storage_service.MakeBucket(bucket) storage_service.AclBucket(benchmark_spec.gcp_service_account, gcs.WRITER, bucket) else: benchmark_spec.model_dir = '/tmp' def CreateMetadataDict(benchmark_spec): """Create metadata dict to be used in run results. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: metadata dict """ metadata = { 'use_tpu': bool(benchmark_spec.tpus), 'data_dir': benchmark_spec.data_dir, 'model_dir': benchmark_spec.model_dir, 'train_steps': benchmark_spec.train_steps, 'eval_steps': benchmark_spec.eval_steps, 'commit': cloud_tpu_models.GetCommit(benchmark_spec.vms[0]), 'iterations': benchmark_spec.iterations, 'num_train_images': benchmark_spec.num_train_images, 'num_eval_images': benchmark_spec.num_eval_images, 'train_epochs': benchmark_spec.train_epochs, 'eval_epochs': benchmark_spec.eval_epochs, 'num_examples_per_epoch': benchmark_spec.num_examples_per_epoch, 'train_batch_size': benchmark_spec.batch_size, 'eval_batch_size': benchmark_spec.batch_size } if benchmark_spec.tpus: metadata.update({ 'train_tpu_num_shards': benchmark_spec.tpu_groups['train'].GetNumShards(), 'train_tpu_accelerator_type': benchmark_spec.tpu_groups['train'].GetAcceleratorType() }) return metadata def ExtractThroughput(regex, output, metadata, metric, unit): """Extract throughput from MNIST output. Args: regex: string. Regular expression. output: MNIST output metadata: dict. Additional metadata to include with the sample. metric: string. Name of the metric within the benchmark. unit: string. Units for 'value'. Returns: samples containing the throughput """ matches = regex_util.ExtractAllMatches(regex, output) samples = [] for index, value in enumerate(matches): metadata_with_index = copy.deepcopy(metadata) metadata_with_index['index'] = index samples.append(sample.Sample(metric, float(value), unit, metadata_with_index)) return samples def MakeSamplesFromTrainOutput(metadata, output, elapsed_seconds, step): """Create a sample containing training metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. step: int, the global steps in the training process. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing training metrics, current step, elapsed seconds """ samples = [] metadata_copy = metadata.copy() metadata_copy['step'] = int(step) metadata_copy['epoch'] = step / metadata['num_examples_per_epoch'] metadata_copy['elapsed_seconds'] = elapsed_seconds get_mean = lambda matches: sum(float(x) for x in matches) / len(matches) loss = get_mean(regex_util.ExtractAllMatches( r'Loss for final step: (\d+\.\d+)', output)) samples.append(sample.Sample('Loss', float(loss), '', metadata_copy)) if 'global_step/sec: ' in output:
return samples def MakeSamplesFromEvalOutput(metadata, output, elapsed_seconds): """Create a sample containing evaluation metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing evaluation metrics """ pattern = (r'Saving dict for global step \d+: accuracy = (\d+\.\d+), ' r'global_step = (\d+), loss = (\d+\.\d+)') accuracy, step, loss = regex_util.ExtractAllMatches(pattern, output).pop() metadata_copy = metadata.copy() step = int(step) metadata_copy['step'] = step num_examples_per_epoch = metadata['num_examples_per_epoch'] metadata_copy['epoch'] = step / num_examples_per_epoch metadata_copy['elapsed_seconds'] = elapsed_seconds return [sample.Sample('Eval Loss', float(loss), '', metadata_copy), sample.Sample('Accuracy', float(accuracy) * 100, '%', metadata_copy)] def Run(benchmark_spec): """Run MNIST on the cluster. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: A list of sample.Sample objects. """ _UpdateBenchmarkSpecWithFlags
global_step_sec = get_mean(regex_util.ExtractAllMatches( r'global_step/sec: (\S+)', output)) samples.append(sample.Sample( 'Global Steps Per Second', global_step_sec, 'global_steps/sec', metadata_copy)) examples_sec = global_step_sec * metadata['train_batch_size'] if 'examples/sec: ' in output: examples_sec_log = get_mean(regex_util.ExtractAllMatches( r'examples/sec: (\S+)', output)) precision = abs(examples_sec_log - examples_sec) / examples_sec_log assert precision < EXAMPLES_PER_SECOND_PRECISION, 'examples/sec is wrong.' examples_sec = examples_sec_log samples.append(sample.Sample('Examples Per Second', examples_sec, 'examples/sec', metadata_copy))
conditional_block
mnist_benchmark.py
eastus """ GCP_ENV = 'PATH=/tmp/pkb/google-cloud-sdk/bin:$PATH' flags.DEFINE_string('mnist_data_dir', None, 'mnist train file for tensorflow') flags.DEFINE_string('imagenet_data_dir', 'gs://cloud-tpu-test-datasets/fake_imagenet', 'Directory where the input data is stored') flags.DEFINE_string( 't2t_data_dir', None, 'Directory where the input data is stored for tensor2tensor') flags.DEFINE_integer('imagenet_num_train_images', 1281167, 'Size of ImageNet training data set.') flags.DEFINE_integer('imagenet_num_eval_images', 50000, 'Size of ImageNet validation data set.') flags.DEFINE_integer('mnist_num_train_images', 55000, 'Size of MNIST training data set.') flags.DEFINE_integer('mnist_num_eval_images', 5000, 'Size of MNIST validation data set.') flags.DEFINE_integer('mnist_train_epochs', 37, 'Total number of training echos', lower_bound=1) flags.DEFINE_integer( 'mnist_eval_epochs', 0, 'Total number of evaluation epochs. If `0`, evaluation ' 'after training is skipped.') flags.DEFINE_integer('tpu_iterations', 500, 'Number of iterations per TPU training loop.') flags.DEFINE_integer('mnist_batch_size', 1024, 'Mini-batch size for the training. Note that this ' 'is the global batch size and not the per-shard batch.') flags.DEFINE_enum('tpu_precision', 'bfloat16', ['bfloat16', 'float32'], 'Precision to use') EXAMPLES_PER_SECOND_PRECISION = 0.01 def GetConfig(user_config): """Load and return benchmark config. Args: user_config: user supplied configuration (flags and config file) Returns: loaded benchmark configuration """ return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME) def _UpdateBenchmarkSpecWithFlags(benchmark_spec): """Update the benchmark_spec with supplied command line flags. Args: benchmark_spec: benchmark specification to update """ benchmark_spec.data_dir = FLAGS.mnist_data_dir benchmark_spec.iterations = FLAGS.tpu_iterations benchmark_spec.gcp_service_account = FLAGS.gcp_service_account benchmark_spec.batch_size = FLAGS.mnist_batch_size benchmark_spec.num_train_images = FLAGS.mnist_num_train_images benchmark_spec.num_eval_images = FLAGS.mnist_num_eval_images benchmark_spec.num_examples_per_epoch = ( float(benchmark_spec.num_train_images) / benchmark_spec.batch_size) benchmark_spec.train_epochs = FLAGS.mnist_train_epochs benchmark_spec.train_steps = int( benchmark_spec.train_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.eval_epochs = FLAGS.mnist_eval_epochs benchmark_spec.eval_steps = int( benchmark_spec.eval_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.precision = FLAGS.tpu_precision benchmark_spec.env_cmd = 'export PYTHONPATH=$PYTHONPATH:$PWD/tpu/models' def Prepare(benchmark_spec): """Install and set up MNIST on the target vm. Args: benchmark_spec: The benchmark specification """ benchmark_spec.always_call_cleanup = True _UpdateBenchmarkSpecWithFlags(benchmark_spec) vm = benchmark_spec.vms[0] if not benchmark_spec.tpus: vm.Install('tensorflow') vm.Install('cloud_tpu_models') vm.Install('tensorflow_models') if benchmark_spec.tpus: storage_service = gcs.GoogleCloudStorageService() benchmark_spec.storage_service = storage_service bucket = 'pkb{}'.format(FLAGS.run_uri) benchmark_spec.bucket = bucket benchmark_spec.model_dir = 'gs://{}'.format(bucket) location = benchmark_spec.tpu_groups['train'].GetZone() storage_service.PrepareService(util.GetRegionFromZone(location)) storage_service.MakeBucket(bucket) storage_service.AclBucket(benchmark_spec.gcp_service_account, gcs.WRITER, bucket) else: benchmark_spec.model_dir = '/tmp' def CreateMetadataDict(benchmark_spec): """Create metadata dict to be used in run results. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: metadata dict """ metadata = { 'use_tpu': bool(benchmark_spec.tpus), 'data_dir': benchmark_spec.data_dir, 'model_dir': benchmark_spec.model_dir, 'train_steps': benchmark_spec.train_steps, 'eval_steps': benchmark_spec.eval_steps, 'commit': cloud_tpu_models.GetCommit(benchmark_spec.vms[0]), 'iterations': benchmark_spec.iterations, 'num_train_images': benchmark_spec.num_train_images, 'num_eval_images': benchmark_spec.num_eval_images, 'train_epochs': benchmark_spec.train_epochs, 'eval_epochs': benchmark_spec.eval_epochs, 'num_examples_per_epoch': benchmark_spec.num_examples_per_epoch, 'train_batch_size': benchmark_spec.batch_size, 'eval_batch_size': benchmark_spec.batch_size } if benchmark_spec.tpus: metadata.update({ 'train_tpu_num_shards': benchmark_spec.tpu_groups['train'].GetNumShards(), 'train_tpu_accelerator_type': benchmark_spec.tpu_groups['train'].GetAcceleratorType() }) return metadata def ExtractThroughput(regex, output, metadata, metric, unit): """Extract throughput from MNIST output. Args: regex: string. Regular expression. output: MNIST output metadata: dict. Additional metadata to include with the sample. metric: string. Name of the metric within the benchmark. unit: string. Units for 'value'. Returns: samples containing the throughput
metadata_with_index = copy.deepcopy(metadata) metadata_with_index['index'] = index samples.append(sample.Sample(metric, float(value), unit, metadata_with_index)) return samples def MakeSamplesFromTrainOutput(metadata, output, elapsed_seconds, step): """Create a sample containing training metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. step: int, the global steps in the training process. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing training metrics, current step, elapsed seconds """ samples = [] metadata_copy = metadata.copy() metadata_copy['step'] = int(step) metadata_copy['epoch'] = step / metadata['num_examples_per_epoch'] metadata_copy['elapsed_seconds'] = elapsed_seconds get_mean = lambda matches: sum(float(x) for x in matches) / len(matches) loss = get_mean(regex_util.ExtractAllMatches( r'Loss for final step: (\d+\.\d+)', output)) samples.append(sample.Sample('Loss', float(loss), '', metadata_copy)) if 'global_step/sec: ' in output: global_step_sec = get_mean(regex_util.ExtractAllMatches( r'global_step/sec: (\S+)', output)) samples.append(sample.Sample( 'Global Steps Per Second', global_step_sec, 'global_steps/sec', metadata_copy)) examples_sec = global_step_sec * metadata['train_batch_size'] if 'examples/sec: ' in output: examples_sec_log = get_mean(regex_util.ExtractAllMatches( r'examples/sec: (\S+)', output)) precision = abs(examples_sec_log - examples_sec) / examples_sec_log assert precision < EXAMPLES_PER_SECOND_PRECISION, 'examples/sec is wrong.' examples_sec = examples_sec_log samples.append(sample.Sample('Examples Per Second', examples_sec, 'examples/sec', metadata_copy)) return samples def MakeSamplesFromEvalOutput(metadata, output, elapsed_seconds): """Create a sample containing evaluation metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing evaluation metrics """ pattern = (r'Saving dict for global step \d+: accuracy = (\d+\.\d+), ' r'global_step = (\d+), loss = (\d+\.\d+)') accuracy, step, loss = regex_util.ExtractAllMatches(pattern, output).pop() metadata_copy = metadata.copy() step = int(step) metadata_copy['step'] = step num_examples_per_epoch = metadata['num_examples_per_epoch'] metadata_copy['epoch'] = step / num_examples_per_epoch metadata_copy['elapsed_seconds'] = elapsed_seconds return [sample.Sample('Eval Loss', float(loss), '', metadata_copy), sample.Sample('Accuracy', float(accuracy) * 100, '%', metadata_copy)] def Run(benchmark_spec): """Run MNIST on the cluster. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: A list of sample.Sample objects. """ _UpdateBenchmarkSpecWithFlags(b
""" matches = regex_util.ExtractAllMatches(regex, output) samples = [] for index, value in enumerate(matches):
random_line_split
mnist_benchmark.py
eastus """ GCP_ENV = 'PATH=/tmp/pkb/google-cloud-sdk/bin:$PATH' flags.DEFINE_string('mnist_data_dir', None, 'mnist train file for tensorflow') flags.DEFINE_string('imagenet_data_dir', 'gs://cloud-tpu-test-datasets/fake_imagenet', 'Directory where the input data is stored') flags.DEFINE_string( 't2t_data_dir', None, 'Directory where the input data is stored for tensor2tensor') flags.DEFINE_integer('imagenet_num_train_images', 1281167, 'Size of ImageNet training data set.') flags.DEFINE_integer('imagenet_num_eval_images', 50000, 'Size of ImageNet validation data set.') flags.DEFINE_integer('mnist_num_train_images', 55000, 'Size of MNIST training data set.') flags.DEFINE_integer('mnist_num_eval_images', 5000, 'Size of MNIST validation data set.') flags.DEFINE_integer('mnist_train_epochs', 37, 'Total number of training echos', lower_bound=1) flags.DEFINE_integer( 'mnist_eval_epochs', 0, 'Total number of evaluation epochs. If `0`, evaluation ' 'after training is skipped.') flags.DEFINE_integer('tpu_iterations', 500, 'Number of iterations per TPU training loop.') flags.DEFINE_integer('mnist_batch_size', 1024, 'Mini-batch size for the training. Note that this ' 'is the global batch size and not the per-shard batch.') flags.DEFINE_enum('tpu_precision', 'bfloat16', ['bfloat16', 'float32'], 'Precision to use') EXAMPLES_PER_SECOND_PRECISION = 0.01 def GetConfig(user_config): """Load and return benchmark config. Args: user_config: user supplied configuration (flags and config file) Returns: loaded benchmark configuration """ return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME) def
(benchmark_spec): """Update the benchmark_spec with supplied command line flags. Args: benchmark_spec: benchmark specification to update """ benchmark_spec.data_dir = FLAGS.mnist_data_dir benchmark_spec.iterations = FLAGS.tpu_iterations benchmark_spec.gcp_service_account = FLAGS.gcp_service_account benchmark_spec.batch_size = FLAGS.mnist_batch_size benchmark_spec.num_train_images = FLAGS.mnist_num_train_images benchmark_spec.num_eval_images = FLAGS.mnist_num_eval_images benchmark_spec.num_examples_per_epoch = ( float(benchmark_spec.num_train_images) / benchmark_spec.batch_size) benchmark_spec.train_epochs = FLAGS.mnist_train_epochs benchmark_spec.train_steps = int( benchmark_spec.train_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.eval_epochs = FLAGS.mnist_eval_epochs benchmark_spec.eval_steps = int( benchmark_spec.eval_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.precision = FLAGS.tpu_precision benchmark_spec.env_cmd = 'export PYTHONPATH=$PYTHONPATH:$PWD/tpu/models' def Prepare(benchmark_spec): """Install and set up MNIST on the target vm. Args: benchmark_spec: The benchmark specification """ benchmark_spec.always_call_cleanup = True _UpdateBenchmarkSpecWithFlags(benchmark_spec) vm = benchmark_spec.vms[0] if not benchmark_spec.tpus: vm.Install('tensorflow') vm.Install('cloud_tpu_models') vm.Install('tensorflow_models') if benchmark_spec.tpus: storage_service = gcs.GoogleCloudStorageService() benchmark_spec.storage_service = storage_service bucket = 'pkb{}'.format(FLAGS.run_uri) benchmark_spec.bucket = bucket benchmark_spec.model_dir = 'gs://{}'.format(bucket) location = benchmark_spec.tpu_groups['train'].GetZone() storage_service.PrepareService(util.GetRegionFromZone(location)) storage_service.MakeBucket(bucket) storage_service.AclBucket(benchmark_spec.gcp_service_account, gcs.WRITER, bucket) else: benchmark_spec.model_dir = '/tmp' def CreateMetadataDict(benchmark_spec): """Create metadata dict to be used in run results. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: metadata dict """ metadata = { 'use_tpu': bool(benchmark_spec.tpus), 'data_dir': benchmark_spec.data_dir, 'model_dir': benchmark_spec.model_dir, 'train_steps': benchmark_spec.train_steps, 'eval_steps': benchmark_spec.eval_steps, 'commit': cloud_tpu_models.GetCommit(benchmark_spec.vms[0]), 'iterations': benchmark_spec.iterations, 'num_train_images': benchmark_spec.num_train_images, 'num_eval_images': benchmark_spec.num_eval_images, 'train_epochs': benchmark_spec.train_epochs, 'eval_epochs': benchmark_spec.eval_epochs, 'num_examples_per_epoch': benchmark_spec.num_examples_per_epoch, 'train_batch_size': benchmark_spec.batch_size, 'eval_batch_size': benchmark_spec.batch_size } if benchmark_spec.tpus: metadata.update({ 'train_tpu_num_shards': benchmark_spec.tpu_groups['train'].GetNumShards(), 'train_tpu_accelerator_type': benchmark_spec.tpu_groups['train'].GetAcceleratorType() }) return metadata def ExtractThroughput(regex, output, metadata, metric, unit): """Extract throughput from MNIST output. Args: regex: string. Regular expression. output: MNIST output metadata: dict. Additional metadata to include with the sample. metric: string. Name of the metric within the benchmark. unit: string. Units for 'value'. Returns: samples containing the throughput """ matches = regex_util.ExtractAllMatches(regex, output) samples = [] for index, value in enumerate(matches): metadata_with_index = copy.deepcopy(metadata) metadata_with_index['index'] = index samples.append(sample.Sample(metric, float(value), unit, metadata_with_index)) return samples def MakeSamplesFromTrainOutput(metadata, output, elapsed_seconds, step): """Create a sample containing training metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. step: int, the global steps in the training process. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing training metrics, current step, elapsed seconds """ samples = [] metadata_copy = metadata.copy() metadata_copy['step'] = int(step) metadata_copy['epoch'] = step / metadata['num_examples_per_epoch'] metadata_copy['elapsed_seconds'] = elapsed_seconds get_mean = lambda matches: sum(float(x) for x in matches) / len(matches) loss = get_mean(regex_util.ExtractAllMatches( r'Loss for final step: (\d+\.\d+)', output)) samples.append(sample.Sample('Loss', float(loss), '', metadata_copy)) if 'global_step/sec: ' in output: global_step_sec = get_mean(regex_util.ExtractAllMatches( r'global_step/sec: (\S+)', output)) samples.append(sample.Sample( 'Global Steps Per Second', global_step_sec, 'global_steps/sec', metadata_copy)) examples_sec = global_step_sec * metadata['train_batch_size'] if 'examples/sec: ' in output: examples_sec_log = get_mean(regex_util.ExtractAllMatches( r'examples/sec: (\S+)', output)) precision = abs(examples_sec_log - examples_sec) / examples_sec_log assert precision < EXAMPLES_PER_SECOND_PRECISION, 'examples/sec is wrong.' examples_sec = examples_sec_log samples.append(sample.Sample('Examples Per Second', examples_sec, 'examples/sec', metadata_copy)) return samples def MakeSamplesFromEvalOutput(metadata, output, elapsed_seconds): """Create a sample containing evaluation metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing evaluation metrics """ pattern = (r'Saving dict for global step \d+: accuracy = (\d+\.\d+), ' r'global_step = (\d+), loss = (\d+\.\d+)') accuracy, step, loss = regex_util.ExtractAllMatches(pattern, output).pop() metadata_copy = metadata.copy() step = int(step) metadata_copy['step'] = step num_examples_per_epoch = metadata['num_examples_per_epoch'] metadata_copy['epoch'] = step / num_examples_per_epoch metadata_copy['elapsed_seconds'] = elapsed_seconds return [sample.Sample('Eval Loss', float(loss), '', metadata_copy), sample.Sample('Accuracy', float(accuracy) * 100, '%', metadata_copy)] def Run(benchmark_spec): """Run MNIST on the cluster. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: A list of sample.Sample objects. """ _UpdateBenchmarkSpecWithFlags
_UpdateBenchmarkSpecWithFlags
identifier_name
mnist_benchmark.py
eastus """ GCP_ENV = 'PATH=/tmp/pkb/google-cloud-sdk/bin:$PATH' flags.DEFINE_string('mnist_data_dir', None, 'mnist train file for tensorflow') flags.DEFINE_string('imagenet_data_dir', 'gs://cloud-tpu-test-datasets/fake_imagenet', 'Directory where the input data is stored') flags.DEFINE_string( 't2t_data_dir', None, 'Directory where the input data is stored for tensor2tensor') flags.DEFINE_integer('imagenet_num_train_images', 1281167, 'Size of ImageNet training data set.') flags.DEFINE_integer('imagenet_num_eval_images', 50000, 'Size of ImageNet validation data set.') flags.DEFINE_integer('mnist_num_train_images', 55000, 'Size of MNIST training data set.') flags.DEFINE_integer('mnist_num_eval_images', 5000, 'Size of MNIST validation data set.') flags.DEFINE_integer('mnist_train_epochs', 37, 'Total number of training echos', lower_bound=1) flags.DEFINE_integer( 'mnist_eval_epochs', 0, 'Total number of evaluation epochs. If `0`, evaluation ' 'after training is skipped.') flags.DEFINE_integer('tpu_iterations', 500, 'Number of iterations per TPU training loop.') flags.DEFINE_integer('mnist_batch_size', 1024, 'Mini-batch size for the training. Note that this ' 'is the global batch size and not the per-shard batch.') flags.DEFINE_enum('tpu_precision', 'bfloat16', ['bfloat16', 'float32'], 'Precision to use') EXAMPLES_PER_SECOND_PRECISION = 0.01 def GetConfig(user_config): """Load and return benchmark config. Args: user_config: user supplied configuration (flags and config file) Returns: loaded benchmark configuration """ return configs.LoadConfig(BENCHMARK_CONFIG, user_config, BENCHMARK_NAME) def _UpdateBenchmarkSpecWithFlags(benchmark_spec): """Update the benchmark_spec with supplied command line flags. Args: benchmark_spec: benchmark specification to update """ benchmark_spec.data_dir = FLAGS.mnist_data_dir benchmark_spec.iterations = FLAGS.tpu_iterations benchmark_spec.gcp_service_account = FLAGS.gcp_service_account benchmark_spec.batch_size = FLAGS.mnist_batch_size benchmark_spec.num_train_images = FLAGS.mnist_num_train_images benchmark_spec.num_eval_images = FLAGS.mnist_num_eval_images benchmark_spec.num_examples_per_epoch = ( float(benchmark_spec.num_train_images) / benchmark_spec.batch_size) benchmark_spec.train_epochs = FLAGS.mnist_train_epochs benchmark_spec.train_steps = int( benchmark_spec.train_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.eval_epochs = FLAGS.mnist_eval_epochs benchmark_spec.eval_steps = int( benchmark_spec.eval_epochs * benchmark_spec.num_examples_per_epoch) benchmark_spec.precision = FLAGS.tpu_precision benchmark_spec.env_cmd = 'export PYTHONPATH=$PYTHONPATH:$PWD/tpu/models' def Prepare(benchmark_spec): """Install and set up MNIST on the target vm. Args: benchmark_spec: The benchmark specification """ benchmark_spec.always_call_cleanup = True _UpdateBenchmarkSpecWithFlags(benchmark_spec) vm = benchmark_spec.vms[0] if not benchmark_spec.tpus: vm.Install('tensorflow') vm.Install('cloud_tpu_models') vm.Install('tensorflow_models') if benchmark_spec.tpus: storage_service = gcs.GoogleCloudStorageService() benchmark_spec.storage_service = storage_service bucket = 'pkb{}'.format(FLAGS.run_uri) benchmark_spec.bucket = bucket benchmark_spec.model_dir = 'gs://{}'.format(bucket) location = benchmark_spec.tpu_groups['train'].GetZone() storage_service.PrepareService(util.GetRegionFromZone(location)) storage_service.MakeBucket(bucket) storage_service.AclBucket(benchmark_spec.gcp_service_account, gcs.WRITER, bucket) else: benchmark_spec.model_dir = '/tmp' def CreateMetadataDict(benchmark_spec): """Create metadata dict to be used in run results. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: metadata dict """ metadata = { 'use_tpu': bool(benchmark_spec.tpus), 'data_dir': benchmark_spec.data_dir, 'model_dir': benchmark_spec.model_dir, 'train_steps': benchmark_spec.train_steps, 'eval_steps': benchmark_spec.eval_steps, 'commit': cloud_tpu_models.GetCommit(benchmark_spec.vms[0]), 'iterations': benchmark_spec.iterations, 'num_train_images': benchmark_spec.num_train_images, 'num_eval_images': benchmark_spec.num_eval_images, 'train_epochs': benchmark_spec.train_epochs, 'eval_epochs': benchmark_spec.eval_epochs, 'num_examples_per_epoch': benchmark_spec.num_examples_per_epoch, 'train_batch_size': benchmark_spec.batch_size, 'eval_batch_size': benchmark_spec.batch_size } if benchmark_spec.tpus: metadata.update({ 'train_tpu_num_shards': benchmark_spec.tpu_groups['train'].GetNumShards(), 'train_tpu_accelerator_type': benchmark_spec.tpu_groups['train'].GetAcceleratorType() }) return metadata def ExtractThroughput(regex, output, metadata, metric, unit): """Extract throughput from MNIST output. Args: regex: string. Regular expression. output: MNIST output metadata: dict. Additional metadata to include with the sample. metric: string. Name of the metric within the benchmark. unit: string. Units for 'value'. Returns: samples containing the throughput """ matches = regex_util.ExtractAllMatches(regex, output) samples = [] for index, value in enumerate(matches): metadata_with_index = copy.deepcopy(metadata) metadata_with_index['index'] = index samples.append(sample.Sample(metric, float(value), unit, metadata_with_index)) return samples def MakeSamplesFromTrainOutput(metadata, output, elapsed_seconds, step): """Create a sample containing training metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. step: int, the global steps in the training process. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing training metrics, current step, elapsed seconds """ samples = [] metadata_copy = metadata.copy() metadata_copy['step'] = int(step) metadata_copy['epoch'] = step / metadata['num_examples_per_epoch'] metadata_copy['elapsed_seconds'] = elapsed_seconds get_mean = lambda matches: sum(float(x) for x in matches) / len(matches) loss = get_mean(regex_util.ExtractAllMatches( r'Loss for final step: (\d+\.\d+)', output)) samples.append(sample.Sample('Loss', float(loss), '', metadata_copy)) if 'global_step/sec: ' in output: global_step_sec = get_mean(regex_util.ExtractAllMatches( r'global_step/sec: (\S+)', output)) samples.append(sample.Sample( 'Global Steps Per Second', global_step_sec, 'global_steps/sec', metadata_copy)) examples_sec = global_step_sec * metadata['train_batch_size'] if 'examples/sec: ' in output: examples_sec_log = get_mean(regex_util.ExtractAllMatches( r'examples/sec: (\S+)', output)) precision = abs(examples_sec_log - examples_sec) / examples_sec_log assert precision < EXAMPLES_PER_SECOND_PRECISION, 'examples/sec is wrong.' examples_sec = examples_sec_log samples.append(sample.Sample('Examples Per Second', examples_sec, 'examples/sec', metadata_copy)) return samples def MakeSamplesFromEvalOutput(metadata, output, elapsed_seconds):
metadata_copy['epoch'] = step / num_examples_per_epoch metadata_copy['elapsed_seconds'] = elapsed_seconds return [sample.Sample('Eval Loss', float(loss), '', metadata_copy), sample.Sample('Accuracy', float(accuracy) * 100, '%', metadata_copy)] def Run(benchmark_spec): """Run MNIST on the cluster. Args: benchmark_spec: The benchmark specification. Contains all data that is required to run the benchmark. Returns: A list of sample.Sample objects. """ _UpdateBenchmarkSpecWithFlags
"""Create a sample containing evaluation metrics. Args: metadata: dict contains all the metadata that reports. output: string, command output elapsed_seconds: float, elapsed seconds from saved checkpoint. Example output: perfkitbenchmarker/tests/linux_benchmarks/mnist_benchmark_test.py Returns: a Sample containing evaluation metrics """ pattern = (r'Saving dict for global step \d+: accuracy = (\d+\.\d+), ' r'global_step = (\d+), loss = (\d+\.\d+)') accuracy, step, loss = regex_util.ExtractAllMatches(pattern, output).pop() metadata_copy = metadata.copy() step = int(step) metadata_copy['step'] = step num_examples_per_epoch = metadata['num_examples_per_epoch']
identifier_body
ext.rs
_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; /// Static call. /// Corresponds to "STACICCALL" opcode in EVM pub fn scall( gas: i64, address: *const u8, input_ptr: *const u8, input_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; // environmental blockchain functions (runtime might not provide all of these!) pub fn blockhash(number: i64, dest: *mut u8); pub fn balance(address: *const u8, dest: *mut u8); pub fn coinbase(dest: *mut u8); pub fn timestamp() -> i64; pub fn blocknumber() -> i64; pub fn difficulty(dest: *mut u8); pub fn gaslimit(dest: *mut u8); #[cfg(feature = "kip6")] pub fn gasleft() -> i64; pub fn sender(dest: *mut u8); pub fn address(dest: *mut u8); pub fn value(dest: *mut u8); pub fn origin(dest: *mut u8); pub fn elog( topic_ptr: *const u8, topic_count: u32, data_ptr: *const u8, data_len: u32 ); pub fn create( endowment: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; #[cfg(feature = "kip4")] pub fn create2( endowment: *const u8, salt: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; pub fn suicide(refund: *const u8) -> !; pub fn ret(ptr: *const u8, len: u32) -> !; pub fn input_length() -> u32; pub fn fetch_input(dst: *mut u8); } } /// Halt execution and register account for deletion. /// /// Value of the current account will be tranfered to `refund` address. pub fn suicide(refund: &Address) -> ! { unsafe { external::suicide(refund.as_ptr()); } } /// Get balance of the given account. /// /// If an account is not registered in the chain yet, /// it is considered as an account with `balance = 0`. pub fn balance(address: &Address) -> U256 { unsafe { fetch_u256(|x| external::balance(address.as_ptr(), x) ) } } /// Create a new account with the given code /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create(endowment: U256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::zero(); unsafe { if external::create( endowment_arr.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } #[cfg(feature = "kip4")] /// Create a new account with the given code and salt, requires KIP-4. /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create2(endowment: U256, salt: H256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::new(); unsafe { if external::create2( endowment_arr.as_ptr(), salt.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } /// Message-call into an account /// /// # Arguments: /// * `gas`- a gas limit for a call. A call execution will halt if call exceed this amount /// * `address` - an address of contract to send a call /// * `value` - a value in Wei to send with a call /// * `input` - a data to send with a call /// * `result` - a mutable reference to be filled with a result data /// /// # Returns: /// /// Call is succeed if it returns `Result::Ok(())` /// If call returns `Result::Err(Error)` it means tha call was failed due to execution halting pub fn call(gas: u64, address: &Address, value: U256, input: &[u8], result: &mut [u8]) -> Result<(), Error> { let mut value_arr = [0u8; 32]; value.to_big_endian(&mut value_arr); unsafe { if external::ccall( gas as i64, address.as_ptr(), value_arr.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but with code at the given `address` /// /// Effectively this function is like calling current account but with /// different code (i.e. like `DELEGATECALL` EVM instruction). /// /// [`call`]: fn.call.html pub fn call_code(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::dcall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but this call and any of it's subcalls are disallowed to modify any storage. /// /// It will return an error in this case. /// /// [`call`]: fn.call.html pub fn static_call(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::scall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Returns hash of the given block or H256::zero() /// /// Only works for 256 most recent blocks excluding current /// Returns H256::zero() in case of failure pub fn block_hash(block_number: u64) -> H256 { let mut res = H256::zero(); unsafe { external::blockhash(block_number as i64, res.as_mut_ptr()) } res } /// Get the current block’s beneficiary address (the current miner account address) pub fn coinbase() -> Address {
/// Get the block's timestamp /// /// It can be viewed as an output of Unix's `time()` function at /// current block's inception. pub fn timestamp() -> u64 { unsafe { external::timestamp() as u64 } } /// Get the block's number /// /// This value represents number of ancestor blocks. /// The genesis block has a number of zero. pub fn block_number() -> u64 { unsafe { external::blocknumber() as u64 } } /// Get the block's difficulty. pub fn difficulty() -> U256 { unsafe { fetch_u256(|x| external::difficulty(x) ) } } /// Get the block's gas limit. pub fn gas_limit() -> U256 { unsafe { fetch_u256(|x| external::gaslimit(x) ) } } #[cfg(feature = "kip6")] /// Get amount of gas left. pub fn gas_left() -> u64 { unsafe { external::gasleft() as u64 } } /// Get caller address /// /// This is the address of the account that is directly responsible for this execution. /// Use [`origin`] to get an address of external account - an original initiator of a transaction pub fn sender() -> Address { unsafe { fetch_address(|x| external::sender(x) ) } } /// Get execution origination address /// /// This is the sender of original transaction. /// It could be only external account, not a contract pub fn origin() -> Address { unsafe { fetch_address(|x|
unsafe { fetch_address(|x| external::coinbase(x) ) } }
identifier_body
ext.rs
_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; /// Static call. /// Corresponds to "STACICCALL" opcode in EVM pub fn scall( gas: i64, address: *const u8, input_ptr: *const u8, input_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; // environmental blockchain functions (runtime might not provide all of these!) pub fn blockhash(number: i64, dest: *mut u8); pub fn balance(address: *const u8, dest: *mut u8); pub fn coinbase(dest: *mut u8); pub fn timestamp() -> i64; pub fn blocknumber() -> i64; pub fn difficulty(dest: *mut u8); pub fn gaslimit(dest: *mut u8); #[cfg(feature = "kip6")] pub fn gasleft() -> i64; pub fn sender(dest: *mut u8); pub fn address(dest: *mut u8); pub fn value(dest: *mut u8); pub fn origin(dest: *mut u8); pub fn elog( topic_ptr: *const u8, topic_count: u32, data_ptr: *const u8, data_len: u32 ); pub fn create( endowment: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; #[cfg(feature = "kip4")] pub fn create2( endowment: *const u8, salt: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; pub fn suicide(refund: *const u8) -> !; pub fn ret(ptr: *const u8, len: u32) -> !; pub fn input_length() -> u32; pub fn fetch_input(dst: *mut u8); } } /// Halt execution and register account for deletion. /// /// Value of the current account will be tranfered to `refund` address. pub fn suicide(refund: &Address) -> ! { unsafe { external::suicide(refund.as_ptr()); } } /// Get balance of the given account. /// /// If an account is not registered in the chain yet, /// it is considered as an account with `balance = 0`. pub fn balance(address: &Address) -> U256 { unsafe { fetch_u256(|x| external::balance(address.as_ptr(), x) ) } } /// Create a new account with the given code /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create(endowment: U256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::zero(); unsafe { if external::create( endowment_arr.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } #[cfg(feature = "kip4")] /// Create a new account with the given code and salt, requires KIP-4. /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create2(endowment: U256, salt: H256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::new(); unsafe { if external::create2( endowment_arr.as_ptr(), salt.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } /// Message-call into an account /// /// # Arguments: /// * `gas`- a gas limit for a call. A call execution will halt if call exceed this amount /// * `address` - an address of contract to send a call /// * `value` - a value in Wei to send with a call /// * `input` - a data to send with a call /// * `result` - a mutable reference to be filled with a result data /// /// # Returns: /// /// Call is succeed if it returns `Result::Ok(())` /// If call returns `Result::Err(Error)` it means tha call was failed due to execution halting pub fn call(gas: u64, address: &Address, value: U256, input: &[u8], result: &mut [u8]) -> Result<(), Error> { let mut value_arr = [0u8; 32]; value.to_big_endian(&mut value_arr); unsafe { if external::ccall( gas as i64, address.as_ptr(), value_arr.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but with code at the given `address` /// /// Effectively this function is like calling current account but with /// different code (i.e. like `DELEGATECALL` EVM instruction). /// /// [`call`]: fn.call.html pub fn call_code(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::dcall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but this call and any of it's subcalls are disallowed to modify any storage. /// /// It will return an error in this case. /// /// [`call`]: fn.call.html pub fn static_call(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::scall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0
else { Err(Error) } } } /// Returns hash of the given block or H256::zero() /// /// Only works for 256 most recent blocks excluding current /// Returns H256::zero() in case of failure pub fn block_hash(block_number: u64) -> H256 { let mut res = H256::zero(); unsafe { external::blockhash(block_number as i64, res.as_mut_ptr()) } res } /// Get the current block’s beneficiary address (the current miner account address) pub fn coinbase() -> Address { unsafe { fetch_address(|x| external::coinbase(x) ) } } /// Get the block's timestamp /// /// It can be viewed as an output of Unix's `time()` function at /// current block's inception. pub fn timestamp() -> u64 { unsafe { external::timestamp() as u64 } } /// Get the block's number /// /// This value represents number of ancestor blocks. /// The genesis block has a number of zero. pub fn block_number() -> u64 { unsafe { external::blocknumber() as u64 } } /// Get the block's difficulty. pub fn difficulty() -> U256 { unsafe { fetch_u256(|x| external::difficulty(x) ) } } /// Get the block's gas limit. pub fn gas_limit() -> U256 { unsafe { fetch_u256(|x| external::gaslimit(x) ) } } #[cfg(feature = "kip6")] /// Get amount of gas left. pub fn gas_left() -> u64 { unsafe { external::gasleft() as u64 } } /// Get caller address /// /// This is the address of the account that is directly responsible for this execution. /// Use [`origin`] to get an address of external account - an original initiator of a transaction pub fn sender() -> Address { unsafe { fetch_address(|x| external::sender(x) ) } } /// Get execution origination address /// /// This is the sender of original transaction. /// It could be only external account, not a contract pub fn origin() -> Address { unsafe { fetch_address(|x
{ Ok(()) }
conditional_block
ext.rs
_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; /// Static call. /// Corresponds to "STACICCALL" opcode in EVM pub fn scall( gas: i64, address: *const u8, input_ptr: *const u8, input_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; // environmental blockchain functions (runtime might not provide all of these!) pub fn blockhash(number: i64, dest: *mut u8); pub fn balance(address: *const u8, dest: *mut u8); pub fn coinbase(dest: *mut u8); pub fn timestamp() -> i64; pub fn blocknumber() -> i64; pub fn difficulty(dest: *mut u8); pub fn gaslimit(dest: *mut u8); #[cfg(feature = "kip6")] pub fn gasleft() -> i64; pub fn sender(dest: *mut u8); pub fn address(dest: *mut u8); pub fn value(dest: *mut u8); pub fn origin(dest: *mut u8); pub fn elog( topic_ptr: *const u8, topic_count: u32, data_ptr: *const u8, data_len: u32 ); pub fn create( endowment: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; #[cfg(feature = "kip4")] pub fn create2( endowment: *const u8, salt: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; pub fn suicide(refund: *const u8) -> !; pub fn ret(ptr: *const u8, len: u32) -> !; pub fn input_length() -> u32; pub fn fetch_input(dst: *mut u8); } } /// Halt execution and register account for deletion. /// /// Value of the current account will be tranfered to `refund` address. pub fn suicide(refund: &Address) -> ! { unsafe { external::suicide(refund.as_ptr()); } } /// Get balance of the given account. /// /// If an account is not registered in the chain yet, /// it is considered as an account with `balance = 0`. pub fn balance(address: &Address) -> U256 { unsafe { fetch_u256(|x| external::balance(address.as_ptr(), x) ) } } /// Create a new account with the given code /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create(endowment: U256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::zero(); unsafe {
if external::create( endowment_arr.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } #[cfg(feature = "kip4")] /// Create a new account with the given code and salt, requires KIP-4. /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create2(endowment: U256, salt: H256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::new(); unsafe { if external::create2( endowment_arr.as_ptr(), salt.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } /// Message-call into an account /// /// # Arguments: /// * `gas`- a gas limit for a call. A call execution will halt if call exceed this amount /// * `address` - an address of contract to send a call /// * `value` - a value in Wei to send with a call /// * `input` - a data to send with a call /// * `result` - a mutable reference to be filled with a result data /// /// # Returns: /// /// Call is succeed if it returns `Result::Ok(())` /// If call returns `Result::Err(Error)` it means tha call was failed due to execution halting pub fn call(gas: u64, address: &Address, value: U256, input: &[u8], result: &mut [u8]) -> Result<(), Error> { let mut value_arr = [0u8; 32]; value.to_big_endian(&mut value_arr); unsafe { if external::ccall( gas as i64, address.as_ptr(), value_arr.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but with code at the given `address` /// /// Effectively this function is like calling current account but with /// different code (i.e. like `DELEGATECALL` EVM instruction). /// /// [`call`]: fn.call.html pub fn call_code(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::dcall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but this call and any of it's subcalls are disallowed to modify any storage. /// /// It will return an error in this case. /// /// [`call`]: fn.call.html pub fn static_call(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::scall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Returns hash of the given block or H256::zero() /// /// Only works for 256 most recent blocks excluding current /// Returns H256::zero() in case of failure pub fn block_hash(block_number: u64) -> H256 { let mut res = H256::zero(); unsafe { external::blockhash(block_number as i64, res.as_mut_ptr()) } res } /// Get the current block’s beneficiary address (the current miner account address) pub fn coinbase() -> Address { unsafe { fetch_address(|x| external::coinbase(x) ) } } /// Get the block's timestamp /// /// It can be viewed as an output of Unix's `time()` function at /// current block's inception. pub fn timestamp() -> u64 { unsafe { external::timestamp() as u64 } } /// Get the block's number /// /// This value represents number of ancestor blocks. /// The genesis block has a number of zero. pub fn block_number() -> u64 { unsafe { external::blocknumber() as u64 } } /// Get the block's difficulty. pub fn difficulty() -> U256 { unsafe { fetch_u256(|x| external::difficulty(x) ) } } /// Get the block's gas limit. pub fn gas_limit() -> U256 { unsafe { fetch_u256(|x| external::gaslimit(x) ) } } #[cfg(feature = "kip6")] /// Get amount of gas left. pub fn gas_left() -> u64 { unsafe { external::gasleft() as u64 } } /// Get caller address /// /// This is the address of the account that is directly responsible for this execution. /// Use [`origin`] to get an address of external account - an original initiator of a transaction pub fn sender() -> Address { unsafe { fetch_address(|x| external::sender(x) ) } } /// Get execution origination address /// /// This is the sender of original transaction. /// It could be only external account, not a contract pub fn origin() -> Address { unsafe { fetch_address(|x|
random_line_split
ext.rs
_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; /// Static call. /// Corresponds to "STACICCALL" opcode in EVM pub fn scall( gas: i64, address: *const u8, input_ptr: *const u8, input_len: u32, result_ptr: *mut u8, result_len: u32, ) -> i32; // environmental blockchain functions (runtime might not provide all of these!) pub fn blockhash(number: i64, dest: *mut u8); pub fn balance(address: *const u8, dest: *mut u8); pub fn coinbase(dest: *mut u8); pub fn timestamp() -> i64; pub fn blocknumber() -> i64; pub fn difficulty(dest: *mut u8); pub fn gaslimit(dest: *mut u8); #[cfg(feature = "kip6")] pub fn gasleft() -> i64; pub fn sender(dest: *mut u8); pub fn address(dest: *mut u8); pub fn value(dest: *mut u8); pub fn origin(dest: *mut u8); pub fn elog( topic_ptr: *const u8, topic_count: u32, data_ptr: *const u8, data_len: u32 ); pub fn create( endowment: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; #[cfg(feature = "kip4")] pub fn create2( endowment: *const u8, salt: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8 ) -> i32; pub fn suicide(refund: *const u8) -> !; pub fn ret(ptr: *const u8, len: u32) -> !; pub fn input_length() -> u32; pub fn fetch_input(dst: *mut u8); } } /// Halt execution and register account for deletion. /// /// Value of the current account will be tranfered to `refund` address. pub fn suicide(refund: &Address) -> ! { unsafe { external::suicide(refund.as_ptr()); } } /// Get balance of the given account. /// /// If an account is not registered in the chain yet, /// it is considered as an account with `balance = 0`. pub fn balance(address: &Address) -> U256 { unsafe { fetch_u256(|x| external::balance(address.as_ptr(), x) ) } } /// Create a new account with the given code /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create(endowment: U256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::zero(); unsafe { if external::create( endowment_arr.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } #[cfg(feature = "kip4")] /// Create a new account with the given code and salt, requires KIP-4. /// /// # Errors /// /// Returns [`Error`] in case contract constructor failed. /// /// [`Error`]: struct.Error.html pub fn create2(endowment: U256, salt: H256, code: &[u8]) -> Result<Address, Error> { let mut endowment_arr = [0u8; 32]; endowment.to_big_endian(&mut endowment_arr); let mut result = Address::new(); unsafe { if external::create2( endowment_arr.as_ptr(), salt.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr() ) == 0 { Ok(result) } else { Err(Error) } } } /// Message-call into an account /// /// # Arguments: /// * `gas`- a gas limit for a call. A call execution will halt if call exceed this amount /// * `address` - an address of contract to send a call /// * `value` - a value in Wei to send with a call /// * `input` - a data to send with a call /// * `result` - a mutable reference to be filled with a result data /// /// # Returns: /// /// Call is succeed if it returns `Result::Ok(())` /// If call returns `Result::Err(Error)` it means tha call was failed due to execution halting pub fn call(gas: u64, address: &Address, value: U256, input: &[u8], result: &mut [u8]) -> Result<(), Error> { let mut value_arr = [0u8; 32]; value.to_big_endian(&mut value_arr); unsafe { if external::ccall( gas as i64, address.as_ptr(), value_arr.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but with code at the given `address` /// /// Effectively this function is like calling current account but with /// different code (i.e. like `DELEGATECALL` EVM instruction). /// /// [`call`]: fn.call.html pub fn call_code(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::dcall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Like [`call`], but this call and any of it's subcalls are disallowed to modify any storage. /// /// It will return an error in this case. /// /// [`call`]: fn.call.html pub fn static_call(gas: u64, address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> { unsafe { if external::scall( gas as i64, address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32 ) == 0 { Ok(()) } else { Err(Error) } } } /// Returns hash of the given block or H256::zero() /// /// Only works for 256 most recent blocks excluding current /// Returns H256::zero() in case of failure pub fn block_hash(block_number: u64) -> H256 { let mut res = H256::zero(); unsafe { external::blockhash(block_number as i64, res.as_mut_ptr()) } res } /// Get the current block’s beneficiary address (the current miner account address) pub fn coinbase() -> Address { unsafe { fetch_address(|x| external::coinbase(x) ) } } /// Get the block's timestamp /// /// It can be viewed as an output of Unix's `time()` function at /// current block's inception. pub fn timestamp() -> u64 { unsafe { external::timestamp() as u64 } } /// Get the block's number /// /// This value represents number of ancestor blocks. /// The genesis block has a number of zero. pub fn bl
-> u64 { unsafe { external::blocknumber() as u64 } } /// Get the block's difficulty. pub fn difficulty() -> U256 { unsafe { fetch_u256(|x| external::difficulty(x) ) } } /// Get the block's gas limit. pub fn gas_limit() -> U256 { unsafe { fetch_u256(|x| external::gaslimit(x) ) } } #[cfg(feature = "kip6")] /// Get amount of gas left. pub fn gas_left() -> u64 { unsafe { external::gasleft() as u64 } } /// Get caller address /// /// This is the address of the account that is directly responsible for this execution. /// Use [`origin`] to get an address of external account - an original initiator of a transaction pub fn sender() -> Address { unsafe { fetch_address(|x| external::sender(x) ) } } /// Get execution origination address /// /// This is the sender of original transaction. /// It could be only external account, not a contract pub fn origin() -> Address { unsafe { fetch_address(|x
ock_number()
identifier_name
networking.py
self.listen_thread.start() def listen(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("", self.port)) s.listen(1) while True: try: clientsock, clientaddr = s.accept() except: traceback.print_exc() continue self.new_connection(Connection(clientsock)) def new_connection(self, connection): self.connections.add(connection) connection.replace_recv_queue(self.recv_queue) def send(self, message, to="*", exclude=()): if isinstance(message, Message): message = struct.pack("!B", message.type_id) + message.pack() if to == "*": to = set(self.connections) for connection in to: if connection in exclude or not connection: continue connection.send(message) def send_chat_message(msg, from_player, to_player=None): if to_player: to_id = to_player.player_id else: to_id = 0xffff m = MChatMessage(msg, from_player.player_id, to_id) if data.THIS_IS_SERVER: if to_player: send(m, to=[to_player.connection]) else: send(m, exclude=[from_player.connection]) else: net.send(m) class Writer(object): def __init__(self, data=None): if data is None: data = StringIO() self.data = data def single(self, format, *values): if format[0] not in "@=<>!": format = "!"+format self.data.write(struct.pack(format, *values)) def multi(self, format, values): self.single("!H", len(values)) if len(format.strip("@=<>!")) > 1: for v in values: self.single(format, *v) else: for v in values: self.single(format, v) def string(self, s): self.multi("s", s) class Reader(object): def __init__(self, data): if not hasattr(data, "read"): data = StringIO(data) self.data = data def single(self, format): if format[0] not in "@=<>!": format = "!"+format return struct.unpack(format, self.data.read(struct.calcsize(format))) def multi(self, format, limit=1000): length = self.single("!H")[0] if length > limit: raise RuntimeError("multi length (%d) is above limit (%d)!!" % (length, limit)) if len(format.strip("@=<>!")) > 1: for i in range(length): yield self.single(format) else: for i in range(length): yield self.single(format)[0] def string(self): return "".join(self.multi("s")) class Message(object): type_id = None struct_format = None attrs = None def __init__(self, *pargs, **kwargs): attrs = list(self.attrs) for arg in pargs: n = attrs.pop(0) setattr(self, n, arg) for n in attrs: setattr(self, n, kwargs.pop(n)) if kwargs: raise TypeError("unexpected keyword argument '%s'" % kwargs.keys()[0]) def values(self): l = [] for a in self.attrs: l.append(getattr(self, a)) return l @classmethod def from_stream(cls, stream): res = struct.unpack(cls.struct_format, stream.read(struct.calcsize(cls.struct_format))) return cls(*res) def pack(self): return struct.pack(self.struct_format, *[getattr(self, n) for n in self.attrs]) def __repr__(self): return "<%s %r>" % (self.__class__.__name__, dict([(a, getattr(self, a)) for a in self.attrs])) # Unit stuff class MUnitPosition(Message): type_id = 100 struct_format = "!HhhbbBB" attrs = ('unit_id', 'x', 'y', 'dir_x', 'dir_y', 'state_id', 'holding_id') # 101 is free class MUnitAddEmblem(Message): type_id = 102 attrs = ("unit_id", "image_name", "animate", "offset") def pack(self): w = Writer() w.single("H", self.unit_id) w.string(self.image_name) w.single("B", self.animate) w.single("hh", *self.offset) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), bool(r.single("B")), r.single("hh")) class MUnitRemoveEmblem(Message): type_id = 103 struct_format = "!H" attrs = ("unit_id",) # TODO support removing only one emblem, instead of all. class MWarmth(Message): type_id = 104 struct_format = "!HB" attrs = ("unit_id", "warmth") class MSnowball(Message): type_id = 105 struct_format = "!HIIIIHHBHH" attrs = ("unit_id", "start_x", "start_y", "end_x", "end_y", "end_tile_x", "end_tile_y", "snowballs_left", 'px', 'py') class MUnitIglooChange(Message): type_id = 109 struct_format = "!HH" attrs = ("unit_id", "building_id") # Building stuff class MBuildingOwner(Message): type_id = 110 struct_format = "!HH" attrs = ('building_id', 'player_id') class MBuildingJeopardy(Message): type_id = 111 struct_format = "!HBH" attrs = ('building_id', 'jeopardy', 'player_taking_over_id') class MBuildingStorage(Message): type_id = 112 struct_format = "!Hhh" attrs = ("building_id", 'fish', 'crystal') class MCreateDynamicMapObject(Message): type_id = 114 attrs = ("dmo_id", "image_name", "position", "obstruction", "hidden", "minimapimage") def
(self): w = Writer() w.single("H", self.dmo_id) w.string(self.image_name) w.single("HH", *self.position) w.single("B", (self.hidden << 0) + (self.obstruction << 1)) w.string(self.minimapimage) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) dmo_id = r.single("H")[0] image_name = r.string() pos = r.single("HH") v = r.single("B")[0] # Two bools are in one byte. o = bool(v & 2) # second bit h = bool(v & 1) # first bit mini = r.string() return cls(dmo_id, image_name, pos, o, h, mini) class MDMOHidden(Message): type_id = 115 struct_format = "!HB" attrs = ("dmo_id", "hidden") class MDMOPosition(Message): type_id = 116 # TODO class MResourceQuantity(Message): type_id = 117 struct_format = "!hhh" attrs = ("tx", "ty", "q") # Player/connection stuff class MNewPlayer(Message): type_id = 120 attrs = ("player_id", "name", "color", "loading") def pack(self): w = Writer() w.single("H", self.player_id) w.string(self.name) w.single("BBB", *self.color) w.single("B", self.loading) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), r.single("BBB"), bool(r.single("B")[0])) class MWhoYouAre(Message): type_id = 121 struct_format = "!H" attrs = ("player_id",) class MSetJob(Message): type_id = 122 attrs = ("pos", "run_mode", "unit_ids") def pack(self): w = Writer() w.single("HH", *self.pos) w.single("B", self.run_mode) w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) pos = r.single("HH") run_mode = bool(r.single("B")[0]) unit_ids = list(r.multi("H")) return cls(pos, run_mode, unit_ids) class MDisbanUnits(Message): type_id = 123 attrs = ("unit_ids",) def pack(self): w = Writer() w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(list(r
pack
identifier_name
networking.py
self.listen_thread.start() def listen(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("", self.port)) s.listen(1) while True: try: clientsock, clientaddr = s.accept() except: traceback.print_exc() continue self.new_connection(Connection(clientsock)) def new_connection(self, connection): self.connections.add(connection) connection.replace_recv_queue(self.recv_queue) def send(self, message, to="*", exclude=()): if isinstance(message, Message): message = struct.pack("!B", message.type_id) + message.pack() if to == "*": to = set(self.connections) for connection in to: if connection in exclude or not connection: continue connection.send(message) def send_chat_message(msg, from_player, to_player=None): if to_player: to_id = to_player.player_id else: to_id = 0xffff m = MChatMessage(msg, from_player.player_id, to_id) if data.THIS_IS_SERVER: if to_player: send(m, to=[to_player.connection]) else: send(m, exclude=[from_player.connection]) else: net.send(m) class Writer(object): def __init__(self, data=None): if data is None: data = StringIO() self.data = data def single(self, format, *values): if format[0] not in "@=<>!": format = "!"+format self.data.write(struct.pack(format, *values)) def multi(self, format, values): self.single("!H", len(values)) if len(format.strip("@=<>!")) > 1: for v in values: self.single(format, *v) else: for v in values: self.single(format, v) def string(self, s): self.multi("s", s) class Reader(object): def __init__(self, data): if not hasattr(data, "read"): data = StringIO(data) self.data = data def single(self, format): if format[0] not in "@=<>!": format = "!"+format return struct.unpack(format, self.data.read(struct.calcsize(format))) def multi(self, format, limit=1000): length = self.single("!H")[0] if length > limit: raise RuntimeError("multi length (%d) is above limit (%d)!!" % (length, limit)) if len(format.strip("@=<>!")) > 1: for i in range(length): yield self.single(format) else: for i in range(length): yield self.single(format)[0] def string(self): return "".join(self.multi("s")) class Message(object): type_id = None struct_format = None attrs = None def __init__(self, *pargs, **kwargs): attrs = list(self.attrs) for arg in pargs: n = attrs.pop(0) setattr(self, n, arg) for n in attrs: setattr(self, n, kwargs.pop(n)) if kwargs: raise TypeError("unexpected keyword argument '%s'" % kwargs.keys()[0]) def values(self): l = [] for a in self.attrs: l.append(getattr(self, a)) return l @classmethod def from_stream(cls, stream): res = struct.unpack(cls.struct_format, stream.read(struct.calcsize(cls.struct_format))) return cls(*res) def pack(self): return struct.pack(self.struct_format, *[getattr(self, n) for n in self.attrs]) def __repr__(self): return "<%s %r>" % (self.__class__.__name__, dict([(a, getattr(self, a)) for a in self.attrs])) # Unit stuff class MUnitPosition(Message): type_id = 100 struct_format = "!HhhbbBB" attrs = ('unit_id', 'x', 'y', 'dir_x', 'dir_y', 'state_id', 'holding_id') # 101 is free class MUnitAddEmblem(Message): type_id = 102 attrs = ("unit_id", "image_name", "animate", "offset") def pack(self): w = Writer() w.single("H", self.unit_id) w.string(self.image_name) w.single("B", self.animate) w.single("hh", *self.offset) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), bool(r.single("B")), r.single("hh")) class MUnitRemoveEmblem(Message): type_id = 103 struct_format = "!H" attrs = ("unit_id",) # TODO support removing only one emblem, instead of all. class MWarmth(Message): type_id = 104 struct_format = "!HB" attrs = ("unit_id", "warmth") class MSnowball(Message): type_id = 105 struct_format = "!HIIIIHHBHH" attrs = ("unit_id", "start_x", "start_y", "end_x", "end_y", "end_tile_x", "end_tile_y", "snowballs_left", 'px', 'py') class MUnitIglooChange(Message): type_id = 109 struct_format = "!HH" attrs = ("unit_id", "building_id") # Building stuff class MBuildingOwner(Message): type_id = 110 struct_format = "!HH" attrs = ('building_id', 'player_id') class MBuildingJeopardy(Message): type_id = 111 struct_format = "!HBH" attrs = ('building_id', 'jeopardy', 'player_taking_over_id') class MBuildingStorage(Message): type_id = 112 struct_format = "!Hhh" attrs = ("building_id", 'fish', 'crystal') class MCreateDynamicMapObject(Message): type_id = 114 attrs = ("dmo_id", "image_name", "position", "obstruction", "hidden", "minimapimage") def pack(self): w = Writer() w.single("H", self.dmo_id) w.string(self.image_name) w.single("HH", *self.position) w.single("B", (self.hidden << 0) + (self.obstruction << 1)) w.string(self.minimapimage) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) dmo_id = r.single("H")[0] image_name = r.string() pos = r.single("HH") v = r.single("B")[0] # Two bools are in one byte. o = bool(v & 2) # second bit h = bool(v & 1) # first bit mini = r.string() return cls(dmo_id, image_name, pos, o, h, mini) class MDMOHidden(Message):
type_id = 116 # TODO class MResourceQuantity(Message): type_id = 117 struct_format = "!hhh" attrs = ("tx", "ty", "q") # Player/connection stuff class MNewPlayer(Message): type_id = 120 attrs = ("player_id", "name", "color", "loading") def pack(self): w = Writer() w.single("H", self.player_id) w.string(self.name) w.single("BBB", *self.color) w.single("B", self.loading) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), r.single("BBB"), bool(r.single("B")[0])) class MWhoYouAre(Message): type_id = 121 struct_format = "!H" attrs = ("player_id",) class MSetJob(Message): type_id = 122 attrs = ("pos", "run_mode", "unit_ids") def pack(self): w = Writer() w.single("HH", *self.pos) w.single("B", self.run_mode) w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) pos = r.single("HH") run_mode = bool(r.single("B")[0]) unit_ids = list(r.multi("H")) return cls(pos, run_mode, unit_ids) class MDisbanUnits(Message): type_id = 123 attrs = ("unit_ids",) def pack(self): w = Writer() w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(list(r.multi
type_id = 115 struct_format = "!HB" attrs = ("dmo_id", "hidden") class MDMOPosition(Message):
random_line_split
networking.py
self.listen_thread.start() def listen(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("", self.port)) s.listen(1) while True: try: clientsock, clientaddr = s.accept() except: traceback.print_exc() continue self.new_connection(Connection(clientsock)) def new_connection(self, connection): self.connections.add(connection) connection.replace_recv_queue(self.recv_queue) def send(self, message, to="*", exclude=()): if isinstance(message, Message): message = struct.pack("!B", message.type_id) + message.pack() if to == "*": to = set(self.connections) for connection in to: if connection in exclude or not connection: continue connection.send(message) def send_chat_message(msg, from_player, to_player=None): if to_player: to_id = to_player.player_id else: to_id = 0xffff m = MChatMessage(msg, from_player.player_id, to_id) if data.THIS_IS_SERVER: if to_player: send(m, to=[to_player.connection]) else: send(m, exclude=[from_player.connection]) else: net.send(m) class Writer(object): def __init__(self, data=None): if data is None: data = StringIO() self.data = data def single(self, format, *values): if format[0] not in "@=<>!":
self.data.write(struct.pack(format, *values)) def multi(self, format, values): self.single("!H", len(values)) if len(format.strip("@=<>!")) > 1: for v in values: self.single(format, *v) else: for v in values: self.single(format, v) def string(self, s): self.multi("s", s) class Reader(object): def __init__(self, data): if not hasattr(data, "read"): data = StringIO(data) self.data = data def single(self, format): if format[0] not in "@=<>!": format = "!"+format return struct.unpack(format, self.data.read(struct.calcsize(format))) def multi(self, format, limit=1000): length = self.single("!H")[0] if length > limit: raise RuntimeError("multi length (%d) is above limit (%d)!!" % (length, limit)) if len(format.strip("@=<>!")) > 1: for i in range(length): yield self.single(format) else: for i in range(length): yield self.single(format)[0] def string(self): return "".join(self.multi("s")) class Message(object): type_id = None struct_format = None attrs = None def __init__(self, *pargs, **kwargs): attrs = list(self.attrs) for arg in pargs: n = attrs.pop(0) setattr(self, n, arg) for n in attrs: setattr(self, n, kwargs.pop(n)) if kwargs: raise TypeError("unexpected keyword argument '%s'" % kwargs.keys()[0]) def values(self): l = [] for a in self.attrs: l.append(getattr(self, a)) return l @classmethod def from_stream(cls, stream): res = struct.unpack(cls.struct_format, stream.read(struct.calcsize(cls.struct_format))) return cls(*res) def pack(self): return struct.pack(self.struct_format, *[getattr(self, n) for n in self.attrs]) def __repr__(self): return "<%s %r>" % (self.__class__.__name__, dict([(a, getattr(self, a)) for a in self.attrs])) # Unit stuff class MUnitPosition(Message): type_id = 100 struct_format = "!HhhbbBB" attrs = ('unit_id', 'x', 'y', 'dir_x', 'dir_y', 'state_id', 'holding_id') # 101 is free class MUnitAddEmblem(Message): type_id = 102 attrs = ("unit_id", "image_name", "animate", "offset") def pack(self): w = Writer() w.single("H", self.unit_id) w.string(self.image_name) w.single("B", self.animate) w.single("hh", *self.offset) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), bool(r.single("B")), r.single("hh")) class MUnitRemoveEmblem(Message): type_id = 103 struct_format = "!H" attrs = ("unit_id",) # TODO support removing only one emblem, instead of all. class MWarmth(Message): type_id = 104 struct_format = "!HB" attrs = ("unit_id", "warmth") class MSnowball(Message): type_id = 105 struct_format = "!HIIIIHHBHH" attrs = ("unit_id", "start_x", "start_y", "end_x", "end_y", "end_tile_x", "end_tile_y", "snowballs_left", 'px', 'py') class MUnitIglooChange(Message): type_id = 109 struct_format = "!HH" attrs = ("unit_id", "building_id") # Building stuff class MBuildingOwner(Message): type_id = 110 struct_format = "!HH" attrs = ('building_id', 'player_id') class MBuildingJeopardy(Message): type_id = 111 struct_format = "!HBH" attrs = ('building_id', 'jeopardy', 'player_taking_over_id') class MBuildingStorage(Message): type_id = 112 struct_format = "!Hhh" attrs = ("building_id", 'fish', 'crystal') class MCreateDynamicMapObject(Message): type_id = 114 attrs = ("dmo_id", "image_name", "position", "obstruction", "hidden", "minimapimage") def pack(self): w = Writer() w.single("H", self.dmo_id) w.string(self.image_name) w.single("HH", *self.position) w.single("B", (self.hidden << 0) + (self.obstruction << 1)) w.string(self.minimapimage) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) dmo_id = r.single("H")[0] image_name = r.string() pos = r.single("HH") v = r.single("B")[0] # Two bools are in one byte. o = bool(v & 2) # second bit h = bool(v & 1) # first bit mini = r.string() return cls(dmo_id, image_name, pos, o, h, mini) class MDMOHidden(Message): type_id = 115 struct_format = "!HB" attrs = ("dmo_id", "hidden") class MDMOPosition(Message): type_id = 116 # TODO class MResourceQuantity(Message): type_id = 117 struct_format = "!hhh" attrs = ("tx", "ty", "q") # Player/connection stuff class MNewPlayer(Message): type_id = 120 attrs = ("player_id", "name", "color", "loading") def pack(self): w = Writer() w.single("H", self.player_id) w.string(self.name) w.single("BBB", *self.color) w.single("B", self.loading) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), r.single("BBB"), bool(r.single("B")[0])) class MWhoYouAre(Message): type_id = 121 struct_format = "!H" attrs = ("player_id",) class MSetJob(Message): type_id = 122 attrs = ("pos", "run_mode", "unit_ids") def pack(self): w = Writer() w.single("HH", *self.pos) w.single("B", self.run_mode) w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) pos = r.single("HH") run_mode = bool(r.single("B")[0]) unit_ids = list(r.multi("H")) return cls(pos, run_mode, unit_ids) class MDisbanUnits(Message): type_id = 123 attrs = ("unit_ids",) def pack(self): w = Writer() w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(list(r.multi
format = "!"+format
conditional_block
networking.py
self.listen_thread.start() def listen(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("", self.port)) s.listen(1) while True: try: clientsock, clientaddr = s.accept() except: traceback.print_exc() continue self.new_connection(Connection(clientsock)) def new_connection(self, connection): self.connections.add(connection) connection.replace_recv_queue(self.recv_queue) def send(self, message, to="*", exclude=()): if isinstance(message, Message): message = struct.pack("!B", message.type_id) + message.pack() if to == "*": to = set(self.connections) for connection in to: if connection in exclude or not connection: continue connection.send(message) def send_chat_message(msg, from_player, to_player=None): if to_player: to_id = to_player.player_id else: to_id = 0xffff m = MChatMessage(msg, from_player.player_id, to_id) if data.THIS_IS_SERVER: if to_player: send(m, to=[to_player.connection]) else: send(m, exclude=[from_player.connection]) else: net.send(m) class Writer(object): def __init__(self, data=None): if data is None: data = StringIO() self.data = data def single(self, format, *values): if format[0] not in "@=<>!": format = "!"+format self.data.write(struct.pack(format, *values)) def multi(self, format, values): self.single("!H", len(values)) if len(format.strip("@=<>!")) > 1: for v in values: self.single(format, *v) else: for v in values: self.single(format, v) def string(self, s): self.multi("s", s) class Reader(object): def __init__(self, data): if not hasattr(data, "read"): data = StringIO(data) self.data = data def single(self, format): if format[0] not in "@=<>!": format = "!"+format return struct.unpack(format, self.data.read(struct.calcsize(format))) def multi(self, format, limit=1000): length = self.single("!H")[0] if length > limit: raise RuntimeError("multi length (%d) is above limit (%d)!!" % (length, limit)) if len(format.strip("@=<>!")) > 1: for i in range(length): yield self.single(format) else: for i in range(length): yield self.single(format)[0] def string(self): return "".join(self.multi("s")) class Message(object): type_id = None struct_format = None attrs = None def __init__(self, *pargs, **kwargs):
def values(self): l = [] for a in self.attrs: l.append(getattr(self, a)) return l @classmethod def from_stream(cls, stream): res = struct.unpack(cls.struct_format, stream.read(struct.calcsize(cls.struct_format))) return cls(*res) def pack(self): return struct.pack(self.struct_format, *[getattr(self, n) for n in self.attrs]) def __repr__(self): return "<%s %r>" % (self.__class__.__name__, dict([(a, getattr(self, a)) for a in self.attrs])) # Unit stuff class MUnitPosition(Message): type_id = 100 struct_format = "!HhhbbBB" attrs = ('unit_id', 'x', 'y', 'dir_x', 'dir_y', 'state_id', 'holding_id') # 101 is free class MUnitAddEmblem(Message): type_id = 102 attrs = ("unit_id", "image_name", "animate", "offset") def pack(self): w = Writer() w.single("H", self.unit_id) w.string(self.image_name) w.single("B", self.animate) w.single("hh", *self.offset) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), bool(r.single("B")), r.single("hh")) class MUnitRemoveEmblem(Message): type_id = 103 struct_format = "!H" attrs = ("unit_id",) # TODO support removing only one emblem, instead of all. class MWarmth(Message): type_id = 104 struct_format = "!HB" attrs = ("unit_id", "warmth") class MSnowball(Message): type_id = 105 struct_format = "!HIIIIHHBHH" attrs = ("unit_id", "start_x", "start_y", "end_x", "end_y", "end_tile_x", "end_tile_y", "snowballs_left", 'px', 'py') class MUnitIglooChange(Message): type_id = 109 struct_format = "!HH" attrs = ("unit_id", "building_id") # Building stuff class MBuildingOwner(Message): type_id = 110 struct_format = "!HH" attrs = ('building_id', 'player_id') class MBuildingJeopardy(Message): type_id = 111 struct_format = "!HBH" attrs = ('building_id', 'jeopardy', 'player_taking_over_id') class MBuildingStorage(Message): type_id = 112 struct_format = "!Hhh" attrs = ("building_id", 'fish', 'crystal') class MCreateDynamicMapObject(Message): type_id = 114 attrs = ("dmo_id", "image_name", "position", "obstruction", "hidden", "minimapimage") def pack(self): w = Writer() w.single("H", self.dmo_id) w.string(self.image_name) w.single("HH", *self.position) w.single("B", (self.hidden << 0) + (self.obstruction << 1)) w.string(self.minimapimage) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) dmo_id = r.single("H")[0] image_name = r.string() pos = r.single("HH") v = r.single("B")[0] # Two bools are in one byte. o = bool(v & 2) # second bit h = bool(v & 1) # first bit mini = r.string() return cls(dmo_id, image_name, pos, o, h, mini) class MDMOHidden(Message): type_id = 115 struct_format = "!HB" attrs = ("dmo_id", "hidden") class MDMOPosition(Message): type_id = 116 # TODO class MResourceQuantity(Message): type_id = 117 struct_format = "!hhh" attrs = ("tx", "ty", "q") # Player/connection stuff class MNewPlayer(Message): type_id = 120 attrs = ("player_id", "name", "color", "loading") def pack(self): w = Writer() w.single("H", self.player_id) w.string(self.name) w.single("BBB", *self.color) w.single("B", self.loading) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(r.single("H")[0], r.string(), r.single("BBB"), bool(r.single("B")[0])) class MWhoYouAre(Message): type_id = 121 struct_format = "!H" attrs = ("player_id",) class MSetJob(Message): type_id = 122 attrs = ("pos", "run_mode", "unit_ids") def pack(self): w = Writer() w.single("HH", *self.pos) w.single("B", self.run_mode) w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) pos = r.single("HH") run_mode = bool(r.single("B")[0]) unit_ids = list(r.multi("H")) return cls(pos, run_mode, unit_ids) class MDisbanUnits(Message): type_id = 123 attrs = ("unit_ids",) def pack(self): w = Writer() w.multi("H", self.unit_ids) return w.data.getvalue() @classmethod def from_stream(cls, stream): r = Reader(stream) return cls(list(r
attrs = list(self.attrs) for arg in pargs: n = attrs.pop(0) setattr(self, n, arg) for n in attrs: setattr(self, n, kwargs.pop(n)) if kwargs: raise TypeError("unexpected keyword argument '%s'" % kwargs.keys()[0])
identifier_body
async_await_basics.rs
}); // Drop the spawner so that our executor knows it is finished and won't // receive more incoming tasks to run. drop(spawner); // Run the executor until the task queue is empty. // This will print "howdy!", pause, and then print "done!". executor.run(); } struct Song { name: String } impl Song { fn new() -> Song { Song { name: String::from("Hotel California") } } } async fn learn_song() -> Song { println!("Learning Song!"); //std::thread::sleep(Duration::from_secs(2)); println!("Good Progress!"); //std::thread::sleep(Duration::from_secs(1)); Song::new() } async fn sing_song(song: Song) { println!("Tune instruments! {}", song.name); //std::thread::sleep(Duration::from_secs(1)); println!("Singing Song! {}", song.name); } async fn dance() { println!("Dance!!") } async fn learn_and_sing() { let song = learn_song().await; sing_song(song).await; } async fn async_main() { let f2 = dance(); let f1 = learn_and_sing(); futures::join!(f2, f1); } // Each time a future is polled, it is polled as part of a "task". Tasks are the top-level futures // that have been submitted to an executor. // Waker provides a wake() method that can be used to tell the executor that the associated task // should be awoken. When wake() is called, the executor knows that the task associated with the Waker // is ready to make progress, and its future should be polled again. // Waker also implements clone() so that it can be copied around and stored. pub struct
{ shared_state: Arc<Mutex<SharedState>>, } /// Shared state between the future and the waiting thread struct SharedState { /// Whether or not the sleep time has elapsed completed: bool, /// The waker for the task that `TimerFuture` is running on. /// The thread can use this after setting `completed = true` to tell /// `TimerFuture`'s task to wake up, see that `completed = true`, and /// move forward. waker: Option<Waker>, } impl Future for TimerFuture { type Output = (); fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { // Look at the shared state to see if the timer has already completed. let mut shared_state = self.shared_state.lock().unwrap(); if shared_state.completed { Poll::Ready(()) } else { // Set waker so that the thread can wake up the current task // when the timer has completed, ensuring that the future is polled // again and sees that `completed = true`. // // It's tempting to do this once rather than repeatedly cloning // the waker each time. However, the `TimerFuture` can move between // tasks on the executor, which could cause a stale waker pointing // to the wrong task, preventing `TimerFuture` from waking up // correctly. // // N.B. it's possible to check for this using the `Waker::will_wake` // function, but we omit that here to keep things simple. shared_state.waker = Some(cx.waker().clone()); Poll::Pending } } } impl TimerFuture { /// Create a new `TimerFuture` which will complete after the provided /// timeout. pub fn new(duration: Duration) -> Self { let shared_state = Arc::new(Mutex::new(SharedState { completed: false, waker: None, })); // Spawn the new thread let thread_shared_state = shared_state.clone(); thread::spawn(move || { thread::sleep(duration); let mut shared_state = thread_shared_state.lock().unwrap(); // Signal that the timer has completed and wake up the last // task on which the future was polled, if one exists. shared_state.completed = true; if let Some(waker) = shared_state.waker.take() { waker.wake() } }); TimerFuture { shared_state } } } /// Task executor that receives tasks off of a channel and runs them. struct Executor { ready_queue: Receiver<Arc<Task>>, } /// `Spawner` spawns new futures onto the task channel. #[derive(Clone)] struct Spawner { task_sender: SyncSender<Arc<Task>>, } /// A future that can reschedule itself to be polled by an `Executor`. struct Task { /// In-progress future that should be pushed to completion. /// /// The `Mutex` is not necessary for correctness, since we only have /// one thread executing tasks at once. However, Rust isn't smart /// enough to know that `future` is only mutated from one thread, /// so we need use the `Mutex` to prove thread-safety. A production /// executor would not need this, and could use `UnsafeCell` instead. future: Mutex<Option<BoxFuture<'static, ()>>>, /// Handle to place the task itself back onto the task queue. task_sender: SyncSender<Arc<Task>>, } fn new_executor_and_spawner() -> (Executor, Spawner) { // Maximum number of tasks to allow queueing in the channel at once. // This is just to make `sync_channel` happy, and wouldn't be present in // a real executor. const MAX_QUEUED_TASKS: usize = 10_000; let (task_sender, ready_queue) = sync_channel(MAX_QUEUED_TASKS); (Executor { ready_queue }, Spawner { task_sender }) } impl Spawner { fn spawn(&self, future: impl Future<Output=()> + 'static + Send) { let future = future.boxed(); let task = Arc::new(Task { future: Mutex::new(Some(future)), task_sender: self.task_sender.clone(), }); self.task_sender.send(task).expect("too many tasks queued"); } } // To poll futures, we'll need to create a Waker. As discussed in the task wakeups section, Wakers // are responsible for scheduling a task to be polled again once wake is called. Remember that Wakers // tell the executor exactly which task has become ready, allowing them to poll just the futures that // are ready to make progress. The easiest way to create a new Waker is by implementing the ArcWake // trait and then using the waker_ref or .into_waker() functions to turn an Arc<impl ArcWake> into a // Waker. impl ArcWake for Task { fn wake_by_ref(arc_self: &Arc<Self>) { // Implement `wake` by sending this task back onto the task channel // so that it will be polled again by the executor. let cloned = arc_self.clone(); arc_self.task_sender.send(cloned).expect("too many tasks queued"); } } impl Executor { fn run(&self) { while let Ok(task) = self.ready_queue.recv() { // Take the future, and if it has not yet completed (is still Some), // poll it in an attempt to complete it. let mut future_slot = task.future.lock().unwrap(); if let Some(mut future) = future_slot.take() { // Create a `LocalWaker` from the task itself let waker = waker_ref(&task); let context = &mut Context::from_waker(&*waker); // `BoxFuture<T>` is a type alias for // `Pin<Box<dyn Future<Output = T> + Send + 'static>>`. // We can get a `Pin<&mut dyn Future + Send + 'static>` // from it by calling the `Pin::as_mut` method. if let Poll::Pending = future.as_mut().poll(context) { // We're not done processing the future, so put it // back in its task to be run again in the future. *future_slot = Some(future); } } } } } // In practice, this problem is solved through integration with an IO-aware system blocking primitive, // such as epoll on Linux, kqueue on FreeBSD and Mac OS, IOCP on Windows, and ports on Fuchsia (all // of which are exposed through the cross-platform Rust crate mio). These primitives all allow a // thread to block on multiple asynchronous IO events, returning once one of the events completes. // In practice, these APIs usually look something like this: /*struct IoBlocker { /* ... */ } struct Event { // An ID uniquely identifying the event that occurred and was listened for. id: usize, // A set of signals to wait for, or which occurred. signals: Signals, } impl IoBlocker { /// Create a new collection of asynchronous IO events to block on. fn new() -> Self { /* ... */ } /// Express an interest in a particular IO event. fn add_io_event_interest( &self, /// The object on which the event will occur io_object: &IoObject, /// A set of signals that may appear on the `io_object` for /// which an event should be triggered, paired with /// an ID to give to events that result from this interest. event
TimerFuture
identifier_name
async_await_basics.rs
!"); }); // Drop the spawner so that our executor knows it is finished and won't // receive more incoming tasks to run. drop(spawner); // Run the executor until the task queue is empty. // This will print "howdy!", pause, and then print "done!". executor.run(); } struct Song { name: String } impl Song { fn new() -> Song { Song { name: String::from("Hotel California") } } } async fn learn_song() -> Song { println!("Learning Song!"); //std::thread::sleep(Duration::from_secs(2)); println!("Good Progress!"); //std::thread::sleep(Duration::from_secs(1)); Song::new() } async fn sing_song(song: Song) { println!("Tune instruments! {}", song.name); //std::thread::sleep(Duration::from_secs(1)); println!("Singing Song! {}", song.name); } async fn dance() { println!("Dance!!") } async fn learn_and_sing() { let song = learn_song().await; sing_song(song).await; } async fn async_main() { let f2 = dance(); let f1 = learn_and_sing(); futures::join!(f2, f1); } // Each time a future is polled, it is polled as part of a "task". Tasks are the top-level futures // that have been submitted to an executor. // Waker provides a wake() method that can be used to tell the executor that the associated task // should be awoken. When wake() is called, the executor knows that the task associated with the Waker // is ready to make progress, and its future should be polled again. // Waker also implements clone() so that it can be copied around and stored. pub struct TimerFuture { shared_state: Arc<Mutex<SharedState>>, } /// Shared state between the future and the waiting thread struct SharedState { /// Whether or not the sleep time has elapsed completed: bool, /// The waker for the task that `TimerFuture` is running on. /// The thread can use this after setting `completed = true` to tell /// `TimerFuture`'s task to wake up, see that `completed = true`, and /// move forward. waker: Option<Waker>, } impl Future for TimerFuture { type Output = (); fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { // Look at the shared state to see if the timer has already completed. let mut shared_state = self.shared_state.lock().unwrap(); if shared_state.completed { Poll::Ready(()) } else { // Set waker so that the thread can wake up the current task // when the timer has completed, ensuring that the future is polled // again and sees that `completed = true`. // // It's tempting to do this once rather than repeatedly cloning // the waker each time. However, the `TimerFuture` can move between // tasks on the executor, which could cause a stale waker pointing // to the wrong task, preventing `TimerFuture` from waking up // correctly.
// function, but we omit that here to keep things simple. shared_state.waker = Some(cx.waker().clone()); Poll::Pending } } } impl TimerFuture { /// Create a new `TimerFuture` which will complete after the provided /// timeout. pub fn new(duration: Duration) -> Self { let shared_state = Arc::new(Mutex::new(SharedState { completed: false, waker: None, })); // Spawn the new thread let thread_shared_state = shared_state.clone(); thread::spawn(move || { thread::sleep(duration); let mut shared_state = thread_shared_state.lock().unwrap(); // Signal that the timer has completed and wake up the last // task on which the future was polled, if one exists. shared_state.completed = true; if let Some(waker) = shared_state.waker.take() { waker.wake() } }); TimerFuture { shared_state } } } /// Task executor that receives tasks off of a channel and runs them. struct Executor { ready_queue: Receiver<Arc<Task>>, } /// `Spawner` spawns new futures onto the task channel. #[derive(Clone)] struct Spawner { task_sender: SyncSender<Arc<Task>>, } /// A future that can reschedule itself to be polled by an `Executor`. struct Task { /// In-progress future that should be pushed to completion. /// /// The `Mutex` is not necessary for correctness, since we only have /// one thread executing tasks at once. However, Rust isn't smart /// enough to know that `future` is only mutated from one thread, /// so we need use the `Mutex` to prove thread-safety. A production /// executor would not need this, and could use `UnsafeCell` instead. future: Mutex<Option<BoxFuture<'static, ()>>>, /// Handle to place the task itself back onto the task queue. task_sender: SyncSender<Arc<Task>>, } fn new_executor_and_spawner() -> (Executor, Spawner) { // Maximum number of tasks to allow queueing in the channel at once. // This is just to make `sync_channel` happy, and wouldn't be present in // a real executor. const MAX_QUEUED_TASKS: usize = 10_000; let (task_sender, ready_queue) = sync_channel(MAX_QUEUED_TASKS); (Executor { ready_queue }, Spawner { task_sender }) } impl Spawner { fn spawn(&self, future: impl Future<Output=()> + 'static + Send) { let future = future.boxed(); let task = Arc::new(Task { future: Mutex::new(Some(future)), task_sender: self.task_sender.clone(), }); self.task_sender.send(task).expect("too many tasks queued"); } } // To poll futures, we'll need to create a Waker. As discussed in the task wakeups section, Wakers // are responsible for scheduling a task to be polled again once wake is called. Remember that Wakers // tell the executor exactly which task has become ready, allowing them to poll just the futures that // are ready to make progress. The easiest way to create a new Waker is by implementing the ArcWake // trait and then using the waker_ref or .into_waker() functions to turn an Arc<impl ArcWake> into a // Waker. impl ArcWake for Task { fn wake_by_ref(arc_self: &Arc<Self>) { // Implement `wake` by sending this task back onto the task channel // so that it will be polled again by the executor. let cloned = arc_self.clone(); arc_self.task_sender.send(cloned).expect("too many tasks queued"); } } impl Executor { fn run(&self) { while let Ok(task) = self.ready_queue.recv() { // Take the future, and if it has not yet completed (is still Some), // poll it in an attempt to complete it. let mut future_slot = task.future.lock().unwrap(); if let Some(mut future) = future_slot.take() { // Create a `LocalWaker` from the task itself let waker = waker_ref(&task); let context = &mut Context::from_waker(&*waker); // `BoxFuture<T>` is a type alias for // `Pin<Box<dyn Future<Output = T> + Send + 'static>>`. // We can get a `Pin<&mut dyn Future + Send + 'static>` // from it by calling the `Pin::as_mut` method. if let Poll::Pending = future.as_mut().poll(context) { // We're not done processing the future, so put it // back in its task to be run again in the future. *future_slot = Some(future); } } } } } // In practice, this problem is solved through integration with an IO-aware system blocking primitive, // such as epoll on Linux, kqueue on FreeBSD and Mac OS, IOCP on Windows, and ports on Fuchsia (all // of which are exposed through the cross-platform Rust crate mio). These primitives all allow a // thread to block on multiple asynchronous IO events, returning once one of the events completes. // In practice, these APIs usually look something like this: /*struct IoBlocker { /* ... */ } struct Event { // An ID uniquely identifying the event that occurred and was listened for. id: usize, // A set of signals to wait for, or which occurred. signals: Signals, } impl IoBlocker { /// Create a new collection of asynchronous IO events to block on. fn new() -> Self { /* ... */ } /// Express an interest in a particular IO event. fn add_io_event_interest( &self, /// The object on which the event will occur io_object: &IoObject, /// A set of signals that may appear on the `io_object` for /// which an event should be triggered, paired with /// an ID to give to events that result from this interest. event
// // N.B. it's possible to check for this using the `Waker::will_wake`
random_line_split
async_await_basics.rs
}); // Drop the spawner so that our executor knows it is finished and won't // receive more incoming tasks to run. drop(spawner); // Run the executor until the task queue is empty. // This will print "howdy!", pause, and then print "done!". executor.run(); } struct Song { name: String } impl Song { fn new() -> Song { Song { name: String::from("Hotel California") } } } async fn learn_song() -> Song { println!("Learning Song!"); //std::thread::sleep(Duration::from_secs(2)); println!("Good Progress!"); //std::thread::sleep(Duration::from_secs(1)); Song::new() } async fn sing_song(song: Song) { println!("Tune instruments! {}", song.name); //std::thread::sleep(Duration::from_secs(1)); println!("Singing Song! {}", song.name); } async fn dance()
async fn learn_and_sing() { let song = learn_song().await; sing_song(song).await; } async fn async_main() { let f2 = dance(); let f1 = learn_and_sing(); futures::join!(f2, f1); } // Each time a future is polled, it is polled as part of a "task". Tasks are the top-level futures // that have been submitted to an executor. // Waker provides a wake() method that can be used to tell the executor that the associated task // should be awoken. When wake() is called, the executor knows that the task associated with the Waker // is ready to make progress, and its future should be polled again. // Waker also implements clone() so that it can be copied around and stored. pub struct TimerFuture { shared_state: Arc<Mutex<SharedState>>, } /// Shared state between the future and the waiting thread struct SharedState { /// Whether or not the sleep time has elapsed completed: bool, /// The waker for the task that `TimerFuture` is running on. /// The thread can use this after setting `completed = true` to tell /// `TimerFuture`'s task to wake up, see that `completed = true`, and /// move forward. waker: Option<Waker>, } impl Future for TimerFuture { type Output = (); fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { // Look at the shared state to see if the timer has already completed. let mut shared_state = self.shared_state.lock().unwrap(); if shared_state.completed { Poll::Ready(()) } else { // Set waker so that the thread can wake up the current task // when the timer has completed, ensuring that the future is polled // again and sees that `completed = true`. // // It's tempting to do this once rather than repeatedly cloning // the waker each time. However, the `TimerFuture` can move between // tasks on the executor, which could cause a stale waker pointing // to the wrong task, preventing `TimerFuture` from waking up // correctly. // // N.B. it's possible to check for this using the `Waker::will_wake` // function, but we omit that here to keep things simple. shared_state.waker = Some(cx.waker().clone()); Poll::Pending } } } impl TimerFuture { /// Create a new `TimerFuture` which will complete after the provided /// timeout. pub fn new(duration: Duration) -> Self { let shared_state = Arc::new(Mutex::new(SharedState { completed: false, waker: None, })); // Spawn the new thread let thread_shared_state = shared_state.clone(); thread::spawn(move || { thread::sleep(duration); let mut shared_state = thread_shared_state.lock().unwrap(); // Signal that the timer has completed and wake up the last // task on which the future was polled, if one exists. shared_state.completed = true; if let Some(waker) = shared_state.waker.take() { waker.wake() } }); TimerFuture { shared_state } } } /// Task executor that receives tasks off of a channel and runs them. struct Executor { ready_queue: Receiver<Arc<Task>>, } /// `Spawner` spawns new futures onto the task channel. #[derive(Clone)] struct Spawner { task_sender: SyncSender<Arc<Task>>, } /// A future that can reschedule itself to be polled by an `Executor`. struct Task { /// In-progress future that should be pushed to completion. /// /// The `Mutex` is not necessary for correctness, since we only have /// one thread executing tasks at once. However, Rust isn't smart /// enough to know that `future` is only mutated from one thread, /// so we need use the `Mutex` to prove thread-safety. A production /// executor would not need this, and could use `UnsafeCell` instead. future: Mutex<Option<BoxFuture<'static, ()>>>, /// Handle to place the task itself back onto the task queue. task_sender: SyncSender<Arc<Task>>, } fn new_executor_and_spawner() -> (Executor, Spawner) { // Maximum number of tasks to allow queueing in the channel at once. // This is just to make `sync_channel` happy, and wouldn't be present in // a real executor. const MAX_QUEUED_TASKS: usize = 10_000; let (task_sender, ready_queue) = sync_channel(MAX_QUEUED_TASKS); (Executor { ready_queue }, Spawner { task_sender }) } impl Spawner { fn spawn(&self, future: impl Future<Output=()> + 'static + Send) { let future = future.boxed(); let task = Arc::new(Task { future: Mutex::new(Some(future)), task_sender: self.task_sender.clone(), }); self.task_sender.send(task).expect("too many tasks queued"); } } // To poll futures, we'll need to create a Waker. As discussed in the task wakeups section, Wakers // are responsible for scheduling a task to be polled again once wake is called. Remember that Wakers // tell the executor exactly which task has become ready, allowing them to poll just the futures that // are ready to make progress. The easiest way to create a new Waker is by implementing the ArcWake // trait and then using the waker_ref or .into_waker() functions to turn an Arc<impl ArcWake> into a // Waker. impl ArcWake for Task { fn wake_by_ref(arc_self: &Arc<Self>) { // Implement `wake` by sending this task back onto the task channel // so that it will be polled again by the executor. let cloned = arc_self.clone(); arc_self.task_sender.send(cloned).expect("too many tasks queued"); } } impl Executor { fn run(&self) { while let Ok(task) = self.ready_queue.recv() { // Take the future, and if it has not yet completed (is still Some), // poll it in an attempt to complete it. let mut future_slot = task.future.lock().unwrap(); if let Some(mut future) = future_slot.take() { // Create a `LocalWaker` from the task itself let waker = waker_ref(&task); let context = &mut Context::from_waker(&*waker); // `BoxFuture<T>` is a type alias for // `Pin<Box<dyn Future<Output = T> + Send + 'static>>`. // We can get a `Pin<&mut dyn Future + Send + 'static>` // from it by calling the `Pin::as_mut` method. if let Poll::Pending = future.as_mut().poll(context) { // We're not done processing the future, so put it // back in its task to be run again in the future. *future_slot = Some(future); } } } } } // In practice, this problem is solved through integration with an IO-aware system blocking primitive, // such as epoll on Linux, kqueue on FreeBSD and Mac OS, IOCP on Windows, and ports on Fuchsia (all // of which are exposed through the cross-platform Rust crate mio). These primitives all allow a // thread to block on multiple asynchronous IO events, returning once one of the events completes. // In practice, these APIs usually look something like this: /*struct IoBlocker { /* ... */ } struct Event { // An ID uniquely identifying the event that occurred and was listened for. id: usize, // A set of signals to wait for, or which occurred. signals: Signals, } impl IoBlocker { /// Create a new collection of asynchronous IO events to block on. fn new() -> Self { /* ... */ } /// Express an interest in a particular IO event. fn add_io_event_interest( &self, /// The object on which the event will occur io_object: &IoObject, /// A set of signals that may appear on the `io_object` for /// which an event should be triggered, paired with /// an ID to give to events that result from this interest.
{ println!("Dance!!") }
identifier_body
async_await_basics.rs
}); // Drop the spawner so that our executor knows it is finished and won't // receive more incoming tasks to run. drop(spawner); // Run the executor until the task queue is empty. // This will print "howdy!", pause, and then print "done!". executor.run(); } struct Song { name: String } impl Song { fn new() -> Song { Song { name: String::from("Hotel California") } } } async fn learn_song() -> Song { println!("Learning Song!"); //std::thread::sleep(Duration::from_secs(2)); println!("Good Progress!"); //std::thread::sleep(Duration::from_secs(1)); Song::new() } async fn sing_song(song: Song) { println!("Tune instruments! {}", song.name); //std::thread::sleep(Duration::from_secs(1)); println!("Singing Song! {}", song.name); } async fn dance() { println!("Dance!!") } async fn learn_and_sing() { let song = learn_song().await; sing_song(song).await; } async fn async_main() { let f2 = dance(); let f1 = learn_and_sing(); futures::join!(f2, f1); } // Each time a future is polled, it is polled as part of a "task". Tasks are the top-level futures // that have been submitted to an executor. // Waker provides a wake() method that can be used to tell the executor that the associated task // should be awoken. When wake() is called, the executor knows that the task associated with the Waker // is ready to make progress, and its future should be polled again. // Waker also implements clone() so that it can be copied around and stored. pub struct TimerFuture { shared_state: Arc<Mutex<SharedState>>, } /// Shared state between the future and the waiting thread struct SharedState { /// Whether or not the sleep time has elapsed completed: bool, /// The waker for the task that `TimerFuture` is running on. /// The thread can use this after setting `completed = true` to tell /// `TimerFuture`'s task to wake up, see that `completed = true`, and /// move forward. waker: Option<Waker>, } impl Future for TimerFuture { type Output = (); fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { // Look at the shared state to see if the timer has already completed. let mut shared_state = self.shared_state.lock().unwrap(); if shared_state.completed { Poll::Ready(()) } else { // Set waker so that the thread can wake up the current task // when the timer has completed, ensuring that the future is polled // again and sees that `completed = true`. // // It's tempting to do this once rather than repeatedly cloning // the waker each time. However, the `TimerFuture` can move between // tasks on the executor, which could cause a stale waker pointing // to the wrong task, preventing `TimerFuture` from waking up // correctly. // // N.B. it's possible to check for this using the `Waker::will_wake` // function, but we omit that here to keep things simple. shared_state.waker = Some(cx.waker().clone()); Poll::Pending } } } impl TimerFuture { /// Create a new `TimerFuture` which will complete after the provided /// timeout. pub fn new(duration: Duration) -> Self { let shared_state = Arc::new(Mutex::new(SharedState { completed: false, waker: None, })); // Spawn the new thread let thread_shared_state = shared_state.clone(); thread::spawn(move || { thread::sleep(duration); let mut shared_state = thread_shared_state.lock().unwrap(); // Signal that the timer has completed and wake up the last // task on which the future was polled, if one exists. shared_state.completed = true; if let Some(waker) = shared_state.waker.take() { waker.wake() } }); TimerFuture { shared_state } } } /// Task executor that receives tasks off of a channel and runs them. struct Executor { ready_queue: Receiver<Arc<Task>>, } /// `Spawner` spawns new futures onto the task channel. #[derive(Clone)] struct Spawner { task_sender: SyncSender<Arc<Task>>, } /// A future that can reschedule itself to be polled by an `Executor`. struct Task { /// In-progress future that should be pushed to completion. /// /// The `Mutex` is not necessary for correctness, since we only have /// one thread executing tasks at once. However, Rust isn't smart /// enough to know that `future` is only mutated from one thread, /// so we need use the `Mutex` to prove thread-safety. A production /// executor would not need this, and could use `UnsafeCell` instead. future: Mutex<Option<BoxFuture<'static, ()>>>, /// Handle to place the task itself back onto the task queue. task_sender: SyncSender<Arc<Task>>, } fn new_executor_and_spawner() -> (Executor, Spawner) { // Maximum number of tasks to allow queueing in the channel at once. // This is just to make `sync_channel` happy, and wouldn't be present in // a real executor. const MAX_QUEUED_TASKS: usize = 10_000; let (task_sender, ready_queue) = sync_channel(MAX_QUEUED_TASKS); (Executor { ready_queue }, Spawner { task_sender }) } impl Spawner { fn spawn(&self, future: impl Future<Output=()> + 'static + Send) { let future = future.boxed(); let task = Arc::new(Task { future: Mutex::new(Some(future)), task_sender: self.task_sender.clone(), }); self.task_sender.send(task).expect("too many tasks queued"); } } // To poll futures, we'll need to create a Waker. As discussed in the task wakeups section, Wakers // are responsible for scheduling a task to be polled again once wake is called. Remember that Wakers // tell the executor exactly which task has become ready, allowing them to poll just the futures that // are ready to make progress. The easiest way to create a new Waker is by implementing the ArcWake // trait and then using the waker_ref or .into_waker() functions to turn an Arc<impl ArcWake> into a // Waker. impl ArcWake for Task { fn wake_by_ref(arc_self: &Arc<Self>) { // Implement `wake` by sending this task back onto the task channel // so that it will be polled again by the executor. let cloned = arc_self.clone(); arc_self.task_sender.send(cloned).expect("too many tasks queued"); } } impl Executor { fn run(&self) { while let Ok(task) = self.ready_queue.recv() { // Take the future, and if it has not yet completed (is still Some), // poll it in an attempt to complete it. let mut future_slot = task.future.lock().unwrap(); if let Some(mut future) = future_slot.take() { // Create a `LocalWaker` from the task itself let waker = waker_ref(&task); let context = &mut Context::from_waker(&*waker); // `BoxFuture<T>` is a type alias for // `Pin<Box<dyn Future<Output = T> + Send + 'static>>`. // We can get a `Pin<&mut dyn Future + Send + 'static>` // from it by calling the `Pin::as_mut` method. if let Poll::Pending = future.as_mut().poll(context)
} } } } // In practice, this problem is solved through integration with an IO-aware system blocking primitive, // such as epoll on Linux, kqueue on FreeBSD and Mac OS, IOCP on Windows, and ports on Fuchsia (all // of which are exposed through the cross-platform Rust crate mio). These primitives all allow a // thread to block on multiple asynchronous IO events, returning once one of the events completes. // In practice, these APIs usually look something like this: /*struct IoBlocker { /* ... */ } struct Event { // An ID uniquely identifying the event that occurred and was listened for. id: usize, // A set of signals to wait for, or which occurred. signals: Signals, } impl IoBlocker { /// Create a new collection of asynchronous IO events to block on. fn new() -> Self { /* ... */ } /// Express an interest in a particular IO event. fn add_io_event_interest( &self, /// The object on which the event will occur io_object: &IoObject, /// A set of signals that may appear on the `io_object` for /// which an event should be triggered, paired with /// an ID to give to events that result from this interest.
{ // We're not done processing the future, so put it // back in its task to be run again in the future. *future_slot = Some(future); }
conditional_block
controller.py
knekkes, samt rom det skal søkes i, (muligens størrelse på hver enkelt arbeidoppgave?) og eventuell hashing-algoritme. Fordeler arbeidoppgaver ved å motta en forespørsel, og sender ut "Kode", tegn, arbeidsnodens søkerom, samt hashing-algoritme. Tar inn resultater (enten svar, eller beskjed om at koden ikke er i søkerommet den ble gitt), og slutter å dele ut oppgaver basert på den koden om den er funnet (returnerer svar til den som sendte dette inn). All kommunikasjon skal foregå via JSON api-kall """ #Webserver from http.server import HTTPServer, BaseHTTPRequestHandler from io import BytesIO #Other needed packages import json from random import random from math import floor #Required variables tasks = [] #Settings port = 80 address = 'localhost' def main(): #def __init__(self, start_point, end_point, keyword, chars, searchwidth, algorithm, id): tasks.append(Task('a','dddd','dddc','abcde',10, 'none', gen_task_id(tasks))) tasks.append(Task('0','5555','300','012345',10, 'none', gen_task_id(tasks))) #Starting webServer print('Starting Webserver on {}:{}'.format(address, port)) httpd = HTTPServer((address, port), webServer) httpd.serve_forever() def get_next_job(): for task in tasks: job = task.get_task_worker() if not job['finished']: if job['free_block']: return job else: task.set_finished() return { 'finished':True, 'free_block':False, 'keyword':'', 'chars':'', 'algorithm':'', 'start_point':'', 'end_point':'' } def get_next_job_json(): return json.dumps(get_next_job()) def gen_task_id(tasks): task_id_unique = False temp_id = floor(random()*10000) while not task_id_unique: task_id_unique = True temp_id = floor(random()*10000) for task in tasks: if task.id == temp_id: task_id_unique = False return temp_id class Task: def __init__(self,
int, end_point, keyword, chars, searchwidth, algorithm, id): self.start_point = start_point # Where to start searching self.end_point = end_point # Where to end the search self.keyword = keyword # What you're searching for self.chars = chars # Characters that can be used self.searchwidth = searchwidth # How large blocks will each worker-node be given at a time self.algorithm = algorithm # How is keyword hashed/encrypted self.id = id #ID to connect results to tasks self.current_point = self.start_point # Will be increased as workers are given blocks to search through self.finished = False self.keyword_found = "" def get_task(self): return { 'id':self.id, 'finished':self.finished, 'algorithm':self.algorithm, 'start_point':self.start_point, 'end_point':self.end_point, 'keyword':self.keyword, 'chars':self.chars, 'searchwidth':self.searchwidth, 'current_point':self.current_point, 'keyword_found':self.keyword_found } def get_task_worker(self): """ Retrieves all info needed for worker and puts it into a dictionary """ start, end = self.get_block() return { 'task_id':self.id, 'finished':self.finished, 'free_block':(start != end), 'keyword':self.keyword, 'chars':self.chars, 'algorithm':self.algorithm, 'start_point':start, 'end_point':end } def get_block(self): """ Retrives the next block to be worked on and updates current_point """ current_value = self.get_value(self.current_point) endpoint_value = self.get_value(self.end_point) worker_start = self.current_point if (endpoint_value - current_value) > self.searchwidth: worker_end = self.get_word_from_value(current_value + self.searchwidth - 1) self.current_point = self.get_word_from_value(current_value + self.searchwidth) else: worker_end = self.end_point self.current_point = worker_end return worker_start, worker_end def get_value(self, str): """ Beregner verdien fra base x-tall til base 10-tall Bruker variablene: str = ordet som det skal beregnes verdi for self.chars = alle tegnene som brukes (f.eks. "0123456789") """ base = len(self.chars) base_placement = len(str) - 1 value = 0 for symbol in str: valueChar = self.chars.find(symbol) value += valueChar * (base ** base_placement) base_placement -= 1 return value def get_word_from_value(self, value): """ Beregner base-x ordet gitt base-10 verdien, og returnerer denne Variabler value = base-10 verdien av kodeordet str = verdiene kodeordet skal beregnes fra self.chars = alle tegnene som brukes (f.eks. "abcdefghij") """ base = len(self.chars) str = "" while value != 0: remainder = value % base value = int((value - remainder) / base) str = self.chars[remainder] + str return str def set_finished(self): self.finished = True class webServer(BaseHTTPRequestHandler): def do_GET(self): """ Handles standard GET-requests and get_job-requests """ path = self.path status_code, res = webServer.handle_get_msg(path) self.send_response(status_code) self.end_headers() self.wfile.write(res.encode()) def handle_get_msg(path): if path == "/get_job": #return 200, get_next_job_json() job = {'job':get_next_job_json()} return 200, webServer.add_json_successfull_status(job) elif path == "/get_tasks": res = [] for task in tasks: res.append(task.get_task()) return 200, json.dumps(res) else: return 404, 'Hello, world! \n{}'.format(path) def do_POST(self): """ Handles all POST-requests, message is decoded in handle_post_msg() """ content_length = int(self.headers['Content-Length']) body = self.rfile.read(content_length) response = BytesIO() try: res = webServer.handle_post_msg(body) print(res) self.send_response(200) except Exception as e: print(e) res = str(e) self.send_response(500) self.end_headers() response.write(res.encode()) self.wfile.write(response.getvalue()) def handle_post_msg(body): """ Takes in a request, converts to dictionary and handles request according to type specified in request Required type subtype Types/subtypes: get job tasks post result create task test post """ request = json.loads(body) type = request['type'] subtype = request['subtype'] if type == 'get': if subtype == 'job': job = {'job':get_next_job()} return webServer.add_json_successfull_status(job) elif subtype == 'tasks': res = [] for task in tasks: res.append(task.get_task()) res = {'tasks':res} return webServer.add_json_successfull_status(res) elif type == 'post': if subtype == 'result': start_point = request['start_point'] end_point = request['end_point'] found_keyword_bool = request['found_keyword_bool'] keyword_found = request['keyword_found'] task_id = request['task_id'] if found_keyword_bool: for task in tasks: if task.id == task_id: task.set_finished() task.keyword_found = keyword_found return webServer.make_json_status(1, "Result delivered successfully") return webServer.make_json_status(0, "Couldn't find task") elif type == 'create': if subtype == 'task': start_point = request['start_point'] end_point = request['end_point'] keyword = request['keyword'] chars = request['chars'] searchwidth = request['searchwidth'] algorithm = request['algorithm'] tasks.append(Task(start_point,end_point,keyword,chars,searchwidth,algorithm,gen_task_id(tasks))) return webServer.make_json_status(1, "Successfully created new task") elif type == 'test': if subtype == 'post': return webServer.make_json_status(1
start_po
identifier_name
controller.py
ommet den ble gitt), og slutter å dele ut oppgaver basert på den koden om den er funnet (returnerer svar til den som sendte dette inn). All kommunikasjon skal foregå via JSON api-kall """ #Webserver from http.server import HTTPServer, BaseHTTPRequestHandler from io import BytesIO #Other needed packages import json from random import random from math import floor #Required variables tasks = [] #Settings port = 80 address = 'localhost' def main(): #def __init__(self, start_point, end_point, keyword, chars, searchwidth, algorithm, id): tasks.append(Task('a','dddd','dddc','abcde',10, 'none', gen_task_id(tasks))) tasks.append(Task('0','5555','300','012345',10, 'none', gen_task_id(tasks))) #Starting webServer print('Starting Webserver on {}:{}'.format(address, port)) httpd = HTTPServer((address, port), webServer) httpd.serve_forever() def get_next_job(): for task in tasks: job = task.get_task_worker() if not job['finished']: if job['free_block']: return job else: task.set_finished() return { 'finished':True, 'free_block':False, 'keyword':'', 'chars':'', 'algorithm':'', 'start_point':'', 'end_point':'' } def get_next_job_json(): return json.dumps(get_next_job()) def gen_task_id(tasks): task_id_unique = False temp_id = floor(random()*10000) while not task_id_unique: task_id_unique = True temp_id = floor(random()*10000) for task in tasks: if task.id == temp_id: task_id_unique = False return temp_id class Task: def __init__(self, start_point, end_point, keyword, chars, searchwidth, algorithm, id): self.start_point = start_point # Where to start searching self.end_point = end_point # Where to end the search self.keyword = keyword # What you're searching for self.chars = chars # Characters that can be used self.searchwidth = searchwidth # How large blocks will each worker-node be given at a time self.algorithm = algorithm # How is keyword hashed/encrypted self.id = id #ID to connect results to tasks self.current_point = self.start_point # Will be increased as workers are given blocks to search through self.finished = False self.keyword_found = "" def get_task(self): return { 'id':self.id, 'finished':self.finished, 'algorithm':self.algorithm, 'start_point':self.start_point, 'end_point':self.end_point, 'keyword':self.keyword, 'chars':self.chars, 'searchwidth':self.searchwidth, 'current_point':self.current_point, 'keyword_found':self.keyword_found } def get_task_worker(self): """ Retrieves all info needed for worker and puts it into a dictionary """ start, end = self.get_block() return { 'task_id':self.id, 'finished':self.finished, 'free_block':(start != end), 'keyword':self.keyword, 'chars':self.chars, 'algorithm':self.algorithm, 'start_point':start, 'end_point':end } def get_block(self): """ Retrives the next block to be worked on and updates current_point """ current_value = self.get_value(self.current_point) endpoint_value = self.get_value(self.end_point) worker_start = self.current_point if (endpoint_value - current_value) > self.searchwidth: worker_end = self.get_word_from_value(current_value + self.searchwidth - 1) self.current_point = self.get_word_from_value(current_value + self.searchwidth) else: worker_end = self.end_point self.current_point = worker_end return worker_start, worker_end def get_value(self, str): """ Beregner verdien fra base x-tall til base 10-tall Bruker variablene: str = ordet som det skal beregnes verdi for self.chars = alle tegnene som brukes (f.eks. "0123456789") """ base = len(self.chars) base_placement = len(str) - 1 value = 0 for symbol in str: valueChar = self.chars.find(symbol) value += valueChar * (base ** base_placement) base_placement -= 1 return value def get_word_from_value(self, value): """ Beregner base-x ordet gitt base-10 verdien, og returnerer denne Variabler value = base-10 verdien av kodeordet str = verdiene kodeordet skal beregnes fra self.chars = alle tegnene som brukes (f.eks. "abcdefghij") """ base = len(self.chars) str = "" while value != 0: remainder = value % base value = int((value - remainder) / base) str = self.chars[remainder] + str return str def set_finished(self): self.finished = True class webServer(BaseHTTPRequestHandler): def do_GET(self): """ Handles standard GET-requests and get_job-requests """ path = self.path status_code, res = webServer.handle_get_msg(path) self.send_response(status_code) self.end_headers() self.wfile.write(res.encode()) def handle_get_msg(path): if path == "/get_job": #return 200, get_next_job_json() job = {'job':get_next_job_json()} return 200, webServer.add_json_successfull_status(job) elif path == "/get_tasks": res = [] for task in tasks: res.append(task.get_task()) return 200, json.dumps(res) else: return 404, 'Hello, world! \n{}'.format(path) def do_POST(self): """ Handles all POST-requests, message is decoded in handle_post_msg() """ content_length = int(self.headers['Content-Length']) body = self.rfile.read(content_length) response = BytesIO() try: res = webServer.handle_post_msg(body) print(res) self.send_response(200) except Exception as e: print(e) res = str(e) self.send_response(500) self.end_headers() response.write(res.encode()) self.wfile.write(response.getvalue()) def handle_post_msg(body): """ Takes in a request, converts to dictionary and handles request according to type specified in request Required type subtype Types/subtypes: get job tasks post result create task test post """ request = json.loads(body) type = request['type'] subtype = request['subtype'] if type == 'get': if subtype == 'job': job = {'job':get_next_job()} return webServer.add_json_successfull_status(job) elif subtype == 'tasks': res = [] for task in tasks: res.append(task.get_task()) res = {'tasks':res} return webServer.add_json_successfull_status(res) elif type == 'post': if subtype == 'result': start_point = request['start_point'] end_point = request['end_point'] found_keyword_bool = request['found_keyword_bool'] keyword_found = request['keyword_found'] task_id = request['task_id'] if found_keyword_bool: for task in tasks: if task.id == task_id: task.set_finished() task.keyword_found = keyword_found return webServer.make_json_status(1, "Result delivered successfully") return webServer.make_json_status(0, "Couldn't find task") elif type == 'create': if subtype == 'task': start_point = request['start_point'] end_point = request['end_point'] keyword = request['keyword'] chars = request['chars'] searchwidth = request['searchwidth'] algorithm = request['algorithm'] tasks.append(Task(start_point,end_point,keyword,chars,searchwidth,algorithm,gen_task_id(tasks))) return webServer.make_json_status(1, "Successfully created new task") elif type == 'test': if subtype == 'post': return webServer.make_json_status(1, "Successful") #If correct type cannot be found return webServer.make_json_status(0, "Task Failed") def make_json_status(status_code, message): return json.dumps({"status_code":status_code, "status":message}) def add_json_successfull_status(res): res['status_code'] = 1 res['status'] = 'Successful' res = json.dumps(res) return res if __name__=='__main__': main()
conditional_block
controller.py
knekkes, samt rom det skal søkes i, (muligens størrelse på hver enkelt arbeidoppgave?) og eventuell hashing-algoritme. Fordeler arbeidoppgaver ved å motta en forespørsel, og sender ut "Kode", tegn, arbeidsnodens søkerom, samt hashing-algoritme. Tar inn resultater (enten svar, eller beskjed om at koden ikke er i søkerommet den ble gitt), og slutter å dele ut oppgaver basert på den koden om den er funnet (returnerer svar til den som sendte dette inn). All kommunikasjon skal foregå via JSON api-kall """ #Webserver from http.server import HTTPServer, BaseHTTPRequestHandler from io import BytesIO #Other needed packages import json from random import random from math import floor #Required variables tasks = [] #Settings port = 80 address = 'localhost' def main(): #def __init__(self, start_point, end_point, keyword, chars, searchwidth, algorithm, id): tasks.append(Task('a','dddd','dddc','abcde',10, 'none', gen_task_id(tasks))) tasks.append(Task('0','5555','300','012345',10, 'none', gen_task_id(tasks))) #Starting webServer print('Starting Webserver on {}:{}'.format(address, port)) httpd = HTTPServer((address, port), webServer) httpd.serve_forever() def get_next_job(): for task in tasks: job = task.get_task_worker() if not job['finished']: if job['free_block']: return job else: task.set_finished() return { 'finished':True, 'free_block':False, 'keyword':'', 'chars':'', 'algorithm':'', 'start_point':'', 'end_point':'' } def get_next_job_json(): return json.dum
k_id(tasks): task_id_unique = False temp_id = floor(random()*10000) while not task_id_unique: task_id_unique = True temp_id = floor(random()*10000) for task in tasks: if task.id == temp_id: task_id_unique = False return temp_id class Task: def __init__(self, start_point, end_point, keyword, chars, searchwidth, algorithm, id): self.start_point = start_point # Where to start searching self.end_point = end_point # Where to end the search self.keyword = keyword # What you're searching for self.chars = chars # Characters that can be used self.searchwidth = searchwidth # How large blocks will each worker-node be given at a time self.algorithm = algorithm # How is keyword hashed/encrypted self.id = id #ID to connect results to tasks self.current_point = self.start_point # Will be increased as workers are given blocks to search through self.finished = False self.keyword_found = "" def get_task(self): return { 'id':self.id, 'finished':self.finished, 'algorithm':self.algorithm, 'start_point':self.start_point, 'end_point':self.end_point, 'keyword':self.keyword, 'chars':self.chars, 'searchwidth':self.searchwidth, 'current_point':self.current_point, 'keyword_found':self.keyword_found } def get_task_worker(self): """ Retrieves all info needed for worker and puts it into a dictionary """ start, end = self.get_block() return { 'task_id':self.id, 'finished':self.finished, 'free_block':(start != end), 'keyword':self.keyword, 'chars':self.chars, 'algorithm':self.algorithm, 'start_point':start, 'end_point':end } def get_block(self): """ Retrives the next block to be worked on and updates current_point """ current_value = self.get_value(self.current_point) endpoint_value = self.get_value(self.end_point) worker_start = self.current_point if (endpoint_value - current_value) > self.searchwidth: worker_end = self.get_word_from_value(current_value + self.searchwidth - 1) self.current_point = self.get_word_from_value(current_value + self.searchwidth) else: worker_end = self.end_point self.current_point = worker_end return worker_start, worker_end def get_value(self, str): """ Beregner verdien fra base x-tall til base 10-tall Bruker variablene: str = ordet som det skal beregnes verdi for self.chars = alle tegnene som brukes (f.eks. "0123456789") """ base = len(self.chars) base_placement = len(str) - 1 value = 0 for symbol in str: valueChar = self.chars.find(symbol) value += valueChar * (base ** base_placement) base_placement -= 1 return value def get_word_from_value(self, value): """ Beregner base-x ordet gitt base-10 verdien, og returnerer denne Variabler value = base-10 verdien av kodeordet str = verdiene kodeordet skal beregnes fra self.chars = alle tegnene som brukes (f.eks. "abcdefghij") """ base = len(self.chars) str = "" while value != 0: remainder = value % base value = int((value - remainder) / base) str = self.chars[remainder] + str return str def set_finished(self): self.finished = True class webServer(BaseHTTPRequestHandler): def do_GET(self): """ Handles standard GET-requests and get_job-requests """ path = self.path status_code, res = webServer.handle_get_msg(path) self.send_response(status_code) self.end_headers() self.wfile.write(res.encode()) def handle_get_msg(path): if path == "/get_job": #return 200, get_next_job_json() job = {'job':get_next_job_json()} return 200, webServer.add_json_successfull_status(job) elif path == "/get_tasks": res = [] for task in tasks: res.append(task.get_task()) return 200, json.dumps(res) else: return 404, 'Hello, world! \n{}'.format(path) def do_POST(self): """ Handles all POST-requests, message is decoded in handle_post_msg() """ content_length = int(self.headers['Content-Length']) body = self.rfile.read(content_length) response = BytesIO() try: res = webServer.handle_post_msg(body) print(res) self.send_response(200) except Exception as e: print(e) res = str(e) self.send_response(500) self.end_headers() response.write(res.encode()) self.wfile.write(response.getvalue()) def handle_post_msg(body): """ Takes in a request, converts to dictionary and handles request according to type specified in request Required type subtype Types/subtypes: get job tasks post result create task test post """ request = json.loads(body) type = request['type'] subtype = request['subtype'] if type == 'get': if subtype == 'job': job = {'job':get_next_job()} return webServer.add_json_successfull_status(job) elif subtype == 'tasks': res = [] for task in tasks: res.append(task.get_task()) res = {'tasks':res} return webServer.add_json_successfull_status(res) elif type == 'post': if subtype == 'result': start_point = request['start_point'] end_point = request['end_point'] found_keyword_bool = request['found_keyword_bool'] keyword_found = request['keyword_found'] task_id = request['task_id'] if found_keyword_bool: for task in tasks: if task.id == task_id: task.set_finished() task.keyword_found = keyword_found return webServer.make_json_status(1, "Result delivered successfully") return webServer.make_json_status(0, "Couldn't find task") elif type == 'create': if subtype == 'task': start_point = request['start_point'] end_point = request['end_point'] keyword = request['keyword'] chars = request['chars'] searchwidth = request['searchwidth'] algorithm = request['algorithm'] tasks.append(Task(start_point,end_point,keyword,chars,searchwidth,algorithm,gen_task_id(tasks))) return webServer.make_json_status(1, "Successfully created new task") elif type == 'test': if subtype == 'post': return webServer.make_json_status
ps(get_next_job()) def gen_tas
identifier_body
controller.py
skal knekkes, samt rom det skal søkes i, (muligens størrelse på hver enkelt arbeidoppgave?) og eventuell hashing-algoritme. Fordeler arbeidoppgaver ved å motta en forespørsel, og sender ut "Kode", tegn, arbeidsnodens søkerom, samt hashing-algoritme. Tar inn resultater (enten svar, eller beskjed om at koden ikke er i søkerommet den ble gitt), og slutter å dele ut oppgaver basert på den koden om den er funnet (returnerer svar til den som sendte dette inn). All kommunikasjon skal foregå via JSON api-kall """ #Webserver from http.server import HTTPServer, BaseHTTPRequestHandler from io import BytesIO #Other needed packages import json from random import random from math import floor #Required variables tasks = [] #Settings port = 80 address = 'localhost' def main(): #def __init__(self, start_point, end_point, keyword, chars, searchwidth, algorithm, id): tasks.append(Task('a','dddd','dddc','abcde',10, 'none', gen_task_id(tasks))) tasks.append(Task('0','5555','300','012345',10, 'none', gen_task_id(tasks))) #Starting webServer print('Starting Webserver on {}:{}'.format(address, port)) httpd = HTTPServer((address, port), webServer) httpd.serve_forever() def get_next_job(): for task in tasks: job = task.get_task_worker() if not job['finished']: if job['free_block']: return job else: task.set_finished() return { 'finished':True, 'free_block':False, 'keyword':'', 'chars':'', 'algorithm':'', 'start_point':'', 'end_point':'' } def get_next_job_json(): return json.dumps(get_next_job()) def gen_task_id(tasks): task_id_unique = False temp_id = floor(random()*10000) while not task_id_unique: task_id_unique = True temp_id = floor(random()*10000) for task in tasks: if task.id == temp_id: task_id_unique = False return temp_id class Task: def __init__(self, start_point, end_point, keyword, chars, searchwidth, algorithm, id): self.start_point = start_point # Where to start searching self.end_point = end_point # Where to end the search self.keyword = keyword # What you're searching for self.chars = chars # Characters that can be used self.searchwidth = searchwidth # How large blocks will each worker-node be given at a time self.algorithm = algorithm # How is keyword hashed/encrypted self.id = id #ID to connect results to tasks
self.current_point = self.start_point # Will be increased as workers are given blocks to search through self.finished = False self.keyword_found = "" def get_task(self): return { 'id':self.id, 'finished':self.finished, 'algorithm':self.algorithm, 'start_point':self.start_point, 'end_point':self.end_point, 'keyword':self.keyword, 'chars':self.chars, 'searchwidth':self.searchwidth, 'current_point':self.current_point, 'keyword_found':self.keyword_found } def get_task_worker(self): """ Retrieves all info needed for worker and puts it into a dictionary """ start, end = self.get_block() return { 'task_id':self.id, 'finished':self.finished, 'free_block':(start != end), 'keyword':self.keyword, 'chars':self.chars, 'algorithm':self.algorithm, 'start_point':start, 'end_point':end } def get_block(self): """ Retrives the next block to be worked on and updates current_point """ current_value = self.get_value(self.current_point) endpoint_value = self.get_value(self.end_point) worker_start = self.current_point if (endpoint_value - current_value) > self.searchwidth: worker_end = self.get_word_from_value(current_value + self.searchwidth - 1) self.current_point = self.get_word_from_value(current_value + self.searchwidth) else: worker_end = self.end_point self.current_point = worker_end return worker_start, worker_end def get_value(self, str): """ Beregner verdien fra base x-tall til base 10-tall Bruker variablene: str = ordet som det skal beregnes verdi for self.chars = alle tegnene som brukes (f.eks. "0123456789") """ base = len(self.chars) base_placement = len(str) - 1 value = 0 for symbol in str: valueChar = self.chars.find(symbol) value += valueChar * (base ** base_placement) base_placement -= 1 return value def get_word_from_value(self, value): """ Beregner base-x ordet gitt base-10 verdien, og returnerer denne Variabler value = base-10 verdien av kodeordet str = verdiene kodeordet skal beregnes fra self.chars = alle tegnene som brukes (f.eks. "abcdefghij") """ base = len(self.chars) str = "" while value != 0: remainder = value % base value = int((value - remainder) / base) str = self.chars[remainder] + str return str def set_finished(self): self.finished = True class webServer(BaseHTTPRequestHandler): def do_GET(self): """ Handles standard GET-requests and get_job-requests """ path = self.path status_code, res = webServer.handle_get_msg(path) self.send_response(status_code) self.end_headers() self.wfile.write(res.encode()) def handle_get_msg(path): if path == "/get_job": #return 200, get_next_job_json() job = {'job':get_next_job_json()} return 200, webServer.add_json_successfull_status(job) elif path == "/get_tasks": res = [] for task in tasks: res.append(task.get_task()) return 200, json.dumps(res) else: return 404, 'Hello, world! \n{}'.format(path) def do_POST(self): """ Handles all POST-requests, message is decoded in handle_post_msg() """ content_length = int(self.headers['Content-Length']) body = self.rfile.read(content_length) response = BytesIO() try: res = webServer.handle_post_msg(body) print(res) self.send_response(200) except Exception as e: print(e) res = str(e) self.send_response(500) self.end_headers() response.write(res.encode()) self.wfile.write(response.getvalue()) def handle_post_msg(body): """ Takes in a request, converts to dictionary and handles request according to type specified in request Required type subtype Types/subtypes: get job tasks post result create task test post """ request = json.loads(body) type = request['type'] subtype = request['subtype'] if type == 'get': if subtype == 'job': job = {'job':get_next_job()} return webServer.add_json_successfull_status(job) elif subtype == 'tasks': res = [] for task in tasks: res.append(task.get_task()) res = {'tasks':res} return webServer.add_json_successfull_status(res) elif type == 'post': if subtype == 'result': start_point = request['start_point'] end_point = request['end_point'] found_keyword_bool = request['found_keyword_bool'] keyword_found = request['keyword_found'] task_id = request['task_id'] if found_keyword_bool: for task in tasks: if task.id == task_id: task.set_finished() task.keyword_found = keyword_found return webServer.make_json_status(1, "Result delivered successfully") return webServer.make_json_status(0, "Couldn't find task") elif type == 'create': if subtype == 'task': start_point = request['start_point'] end_point = request['end_point'] keyword = request['keyword'] chars = request['chars'] searchwidth = request['searchwidth'] algorithm = request['algorithm'] tasks.append(Task(start_point,end_point,keyword,chars,searchwidth,algorithm,gen_task_id(tasks))) return webServer.make_json_status(1, "Successfully created new task") elif type == 'test': if subtype == 'post': return webServer.make_json_status(1
random_line_split
main.rs
ClassStruct<Self>; glib_object_subclass!(); fn new() -> Self { Self { widgets: OnceCell::new(), counter: Cell::new(0), } } } static MUSIC_FOLDER: &str = "musics"; impl ObjectImpl for SimpleWindowPrivate { glib_object_impl!(); // Here we are overriding the glib::Objcet::contructed // method. Its what gets called when we create our Object // and where we can initialize things. fn constructed(&self, obj: &glib::Object) { // ==== MUSIC SELCTOR BOX ===== let combo_box = gtk::ComboBoxTextBuilder::new() .width_request(50) .build(); let all_musics = std::fs::read_dir(MUSIC_FOLDER).unwrap().filter(|e| e.is_ok()).map(|e| e.unwrap()); all_musics.enumerate().for_each(|(idx, v)| { let name = v.path(); let name = name.to_string_lossy().replace(&format!("{}/", MUSIC_FOLDER), ""); println!("{}", name); combo_box.insert(idx as i32, None, &name); }); let combo_box = Arc::new(combo_box); // Audio player handle let audio_player = Arc::new(audio_handler::AudioHandler::new()); self.parent_constructed(obj); let self_ = obj.downcast_ref::<SimpleWindow>().unwrap(); // Basic UI elements let headerbar = gtk::HeaderBar::new(); let increment = gtk::Button::new_with_label("Add meaning to my life"); let reset = gtk::Button::new_with_label("Reset my life"); let no = gtk::Button::new_with_label("no"); let decrement = gtk::Button::new_with_label("Remove meaning from my life ;_;"); let label = gtk::Label::new(Some("What doth life has for you?")); let bbox = gtk::BoxBuilder::new() .orientation(gtk::Orientation::Vertical) .build(); let play_button = gtk::Button::new_with_label("Play"); let pause_button = gtk::Button::new_with_label("Pause"); let tbox = gtk::EntryBuilder::new() .height_request(10) .activates_default(true) .build(); tbox.set_text("I don't know what to do with that textbox DD:"); let test = Arc::new(tbox); let inner_tbox = test.clone(); test.clone().connect_activate(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); inner_tbox.set_text("WHy u pressed enter DDD:"); priv_.widgets.get().unwrap().label.set_text("WHy u pressed enter DDD:"); })); bbox.pack_start(test.as_ref(), false, false, 100); bbox.pack_start(&reset, false, false, 10); bbox.pack_start(&no, false, false, 10); bbox.pack_start(&label, false, false, 10); bbox.pack_start(&play_button, false, false, 10); bbox.pack_start(&pause_button, false, false, 10); bbox.pack_start(combo_box.as_ref(), false, false, 10); headerbar.set_title(Some("This is your life now")); headerbar.set_show_close_button(true); headerbar.pack_start(&increment); headerbar.pack_start(&decrement); let audio_player_clone = audio_player.clone(); let combo_box_clone = combo_box.clone(); // Music buttons closures play_button.connect_clicked(move |_| { let music = combo_box_clone.get_active_text().unwrap(); let music = format!("{}/{}", MUSIC_FOLDER, music.as_str()); audio_player_clone.play_music(music); }); let audio_player_clone = audio_player.clone(); pause_button.connect_clicked(move |_| { audio_player_clone.pause_music(); }); // Connect our method `on_increment_clicked` to be called // when the increment button is clicked. increment.connect_clicked(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); priv_.on_increment_clicked(); })); decrement.connect_clicked(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); priv_.on_decrement_clicked(); })); reset.connect_clicked(clone!(@weak self_ => move |_| { println!("Maybe ;___;"); })); self_.add(&bbox); // self_.add(&label); self_.set_titlebar(Some(&headerbar)); self_.set_default_size(640, 480); self.widgets .set(WindowWidgets { headerbar, label, increment, decrement, reset, }) .expect("Failed to initialize window state"); } } impl SimpleWindowPrivate { fn on_increment_clicked(&self) { self.counter.set(self.counter.get() + 1); let w = self.widgets.get().unwrap(); w.label .set_text(&format!("Your life has {} meaning", self.counter.get())); } fn on_decrement_clicked(&self) { self.counter.set(self.counter.get().wrapping_sub(1)); let w = self.widgets.get().unwrap(); w.label .set_text(&format!("Your life has {} meaning", self.counter.get())); } } impl WidgetImpl for SimpleWindowPrivate {} impl ContainerImpl for SimpleWindowPrivate {} impl BinImpl for SimpleWindowPrivate {} impl WindowImpl for SimpleWindowPrivate {} impl ApplicationWindowImpl for SimpleWindowPrivate {} glib_wrapper! { pub struct SimpleWindow( Object<subclass::simple::InstanceStruct<SimpleWindowPrivate>, subclass::simple::ClassStruct<SimpleWindowPrivate>, SimpleAppWindowClass>) @extends gtk::Widget, gtk::Container, gtk::Bin, gtk::Window, gtk::ApplicationWindow; match fn { get_type => || SimpleWindowPrivate::get_type().to_glib(), } } impl SimpleWindow { pub fn new(app: &gtk::Application) -> Self { glib::Object::new(Self::static_type(), &[("application", app)]) .expect("Failed to create SimpleWindow") .downcast::<SimpleWindow>() .expect("Created SimpleWindow is of wrong type") } } #[derive(Debug)] pub struct SimpleApplicationPrivate { window: OnceCell<SimpleWindow>, } impl ObjectSubclass for SimpleApplicationPrivate { const NAME: &'static str = "SimpleApplicationPrivate"; type ParentType = gtk::Application; type Instance = subclass::simple::InstanceStruct<Self>; type Class = subclass::simple::ClassStruct<Self>; glib_object_subclass!(); fn new() -> Self { Self { window: OnceCell::new(), } } } impl ObjectImpl for SimpleApplicationPrivate { glib_object_impl!(); } // When our application starts, the `startup` signal will be fired. // This gives us a chance to perform initialisation tasks that are not directly // related to showing a new window. After this, depending on how // the application is started, either `activate` or `open` will be called next. impl ApplicationImpl for SimpleApplicationPrivate { // `gio::Application::activate` is what gets called when the // application is launched by the desktop environment and // aksed to present itself. fn activate(&self, app: &gio::Application) { let app = app.downcast_ref::<gtk::Application>().unwrap(); let priv_ = SimpleApplicationPrivate::from_instance(app); let window = priv_ .window .get() .expect("Should always be initiliazed in gio_application_startup"); window.show_all(); window.present(); } // `gio::Application` is bit special. It does not get initialized // when `new` is called and the object created, but rather // once the `startup` signal is emitted and the `gio::Application::startup` // is called. // // Due to this, we create and initialize the `SimpleWindow` widget // here. Widgets can't be created before `startup` has been called. fn startup(&self, app: &gio::Application) { self.parent_startup(app); let app = app.downcast_ref::<gtk::Application>().unwrap(); let priv_ = SimpleApplicationPrivate::from_instance(app); let window = SimpleWindow::new(&app); priv_ .window .set(window) .expect("Failed to initialize application window"); } } impl GtkApplicationImpl for SimpleApplicationPrivate {} glib_wrapper! { pub struct SimpleApplication( Object<subclass::simple::InstanceStruct<SimpleApplicationPrivate>, subclass::simple::ClassStruct<SimpleApplicationPrivate>, SimpleApplicationClass>) @extends gio::Application, gtk::Application; match fn { get_type => || SimpleApplicationPrivate::get_type().to_glib(), } } impl SimpleApplication { pub fn new() -> Self
{ glib::Object::new( Self::static_type(), &[ ("application-id", &"org.gtk-rs.SimpleApplication"), ("flags", &ApplicationFlags::empty()), ], ) .expect("Failed to create SimpleApp") .downcast() .expect("Created simpleapp is of wrong type") }
identifier_body
main.rs
use gio::subclass::application::ApplicationImplExt; use gio::ApplicationFlags; use glib::subclass; use glib::subclass::prelude::*; use glib::translate::*; use gtk::subclass::prelude::*; use once_cell::unsync::OnceCell; use std::cell::Cell; mod audio_handler; #[derive(Debug)] struct WindowWidgets { headerbar: gtk::HeaderBar, increment: gtk::Button, decrement: gtk::Button, reset: gtk::Button, label: gtk::Label, } // This is the private part of our `SimpleWindow` object. // Its where state and widgets are stored when they don't // need to be publicly accesible. #[derive(Debug)] pub struct SimpleWindowPrivate { widgets: OnceCell<WindowWidgets>, counter: Cell<i64>, } impl ObjectSubclass for SimpleWindowPrivate { const NAME: &'static str = "SimpleWindowPrivate"; type ParentType = gtk::ApplicationWindow; type Instance = subclass::simple::InstanceStruct<Self>; type Class = subclass::simple::ClassStruct<Self>; glib_object_subclass!(); fn new() -> Self { Self { widgets: OnceCell::new(), counter: Cell::new(0), } } } static MUSIC_FOLDER: &str = "musics"; impl ObjectImpl for SimpleWindowPrivate { glib_object_impl!(); // Here we are overriding the glib::Objcet::contructed // method. Its what gets called when we create our Object // and where we can initialize things. fn constructed(&self, obj: &glib::Object) { // ==== MUSIC SELCTOR BOX ===== let combo_box = gtk::ComboBoxTextBuilder::new() .width_request(50) .build(); let all_musics = std::fs::read_dir(MUSIC_FOLDER).unwrap().filter(|e| e.is_ok()).map(|e| e.unwrap()); all_musics.enumerate().for_each(|(idx, v)| { let name = v.path(); let name = name.to_string_lossy().replace(&format!("{}/", MUSIC_FOLDER), ""); println!("{}", name); combo_box.insert(idx as i32, None, &name); }); let combo_box = Arc::new(combo_box); // Audio player handle let audio_player = Arc::new(audio_handler::AudioHandler::new()); self.parent_constructed(obj); let self_ = obj.downcast_ref::<SimpleWindow>().unwrap(); // Basic UI elements let headerbar = gtk::HeaderBar::new(); let increment = gtk::Button::new_with_label("Add meaning to my life"); let reset = gtk::Button::new_with_label("Reset my life"); let no = gtk::Button::new_with_label("no"); let decrement = gtk::Button::new_with_label("Remove meaning from my life ;_;"); let label = gtk::Label::new(Some("What doth life has for you?")); let bbox = gtk::BoxBuilder::new() .orientation(gtk::Orientation::Vertical) .build(); let play_button = gtk::Button::new_with_label("Play"); let pause_button = gtk::Button::new_with_label("Pause"); let tbox = gtk::EntryBuilder::new() .height_request(10) .activates_default(true) .build(); tbox.set_text("I don't know what to do with that textbox DD:"); let test = Arc::new(tbox); let inner_tbox = test.clone(); test.clone().connect_activate(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); inner_tbox.set_text("WHy u pressed enter DDD:"); priv_.widgets.get().unwrap().label.set_text("WHy u pressed enter DDD:"); })); bbox.pack_start(test.as_ref(), false, false, 100); bbox.pack_start(&reset, false, false, 10); bbox.pack_start(&no, false, false, 10); bbox.pack_start(&label, false, false, 10); bbox.pack_start(&play_button, false, false, 10); bbox.pack_start(&pause_button, false, false, 10); bbox.pack_start(combo_box.as_ref(), false, false, 10); headerbar.set_title(Some("This is your life now")); headerbar.set_show_close_button(true); headerbar.pack_start(&increment); headerbar.pack_start(&decrement); let audio_player_clone = audio_player.clone(); let combo_box_clone = combo_box.clone(); // Music buttons closures play_button.connect_clicked(move |_| { let music = combo_box_clone.get_active_text().unwrap(); let music = format!("{}/{}", MUSIC_FOLDER, music.as_str()); audio_player_clone.play_music(music); }); let audio_player_clone = audio_player.clone();
audio_player_clone.pause_music(); }); // Connect our method `on_increment_clicked` to be called // when the increment button is clicked. increment.connect_clicked(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); priv_.on_increment_clicked(); })); decrement.connect_clicked(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); priv_.on_decrement_clicked(); })); reset.connect_clicked(clone!(@weak self_ => move |_| { println!("Maybe ;___;"); })); self_.add(&bbox); // self_.add(&label); self_.set_titlebar(Some(&headerbar)); self_.set_default_size(640, 480); self.widgets .set(WindowWidgets { headerbar, label, increment, decrement, reset, }) .expect("Failed to initialize window state"); } } impl SimpleWindowPrivate { fn on_increment_clicked(&self) { self.counter.set(self.counter.get() + 1); let w = self.widgets.get().unwrap(); w.label .set_text(&format!("Your life has {} meaning", self.counter.get())); } fn on_decrement_clicked(&self) { self.counter.set(self.counter.get().wrapping_sub(1)); let w = self.widgets.get().unwrap(); w.label .set_text(&format!("Your life has {} meaning", self.counter.get())); } } impl WidgetImpl for SimpleWindowPrivate {} impl ContainerImpl for SimpleWindowPrivate {} impl BinImpl for SimpleWindowPrivate {} impl WindowImpl for SimpleWindowPrivate {} impl ApplicationWindowImpl for SimpleWindowPrivate {} glib_wrapper! { pub struct SimpleWindow( Object<subclass::simple::InstanceStruct<SimpleWindowPrivate>, subclass::simple::ClassStruct<SimpleWindowPrivate>, SimpleAppWindowClass>) @extends gtk::Widget, gtk::Container, gtk::Bin, gtk::Window, gtk::ApplicationWindow; match fn { get_type => || SimpleWindowPrivate::get_type().to_glib(), } } impl SimpleWindow { pub fn new(app: &gtk::Application) -> Self { glib::Object::new(Self::static_type(), &[("application", app)]) .expect("Failed to create SimpleWindow") .downcast::<SimpleWindow>() .expect("Created SimpleWindow is of wrong type") } } #[derive(Debug)] pub struct SimpleApplicationPrivate { window: OnceCell<SimpleWindow>, } impl ObjectSubclass for SimpleApplicationPrivate { const NAME: &'static str = "SimpleApplicationPrivate"; type ParentType = gtk::Application; type Instance = subclass::simple::InstanceStruct<Self>; type Class = subclass::simple::ClassStruct<Self>; glib_object_subclass!(); fn new() -> Self { Self { window: OnceCell::new(), } } } impl ObjectImpl for SimpleApplicationPrivate { glib_object_impl!(); } // When our application starts, the `startup` signal will be fired. // This gives us a chance to perform initialisation tasks that are not directly // related to showing a new window. After this, depending on how // the application is started, either `activate` or `open` will be called next. impl ApplicationImpl for SimpleApplicationPrivate { // `gio::Application::activate` is what gets called when the // application is launched by the desktop environment and // aksed to present itself. fn activate(&self, app: &gio::Application) { let app = app.downcast_ref::<gtk::Application>().unwrap(); let priv_ = SimpleApplicationPrivate::from_instance(app); let window = priv_ .window .get() .expect("Should always be initiliazed in gio_application_startup"); window.show_all(); window.present(); } // `gio::Application` is bit special. It does not get initialized // when `new` is called and the object created, but rather // once the `startup` signal is emitted and the `gio::Application::startup` // is called. // // Due to this, we create and initialize the `SimpleWindow` widget // here. Widgets can't be created before `startup` has been called. fn startup(&self, app: &gio::Application) { self.parent_startup(app); let app = app.downcast_ref::<
pause_button.connect_clicked(move |_| {
random_line_split
main.rs
use gio::subclass::application::ApplicationImplExt; use gio::ApplicationFlags; use glib::subclass; use glib::subclass::prelude::*; use glib::translate::*; use gtk::subclass::prelude::*; use once_cell::unsync::OnceCell; use std::cell::Cell; mod audio_handler; #[derive(Debug)] struct WindowWidgets { headerbar: gtk::HeaderBar, increment: gtk::Button, decrement: gtk::Button, reset: gtk::Button, label: gtk::Label, } // This is the private part of our `SimpleWindow` object. // Its where state and widgets are stored when they don't // need to be publicly accesible. #[derive(Debug)] pub struct SimpleWindowPrivate { widgets: OnceCell<WindowWidgets>, counter: Cell<i64>, } impl ObjectSubclass for SimpleWindowPrivate { const NAME: &'static str = "SimpleWindowPrivate"; type ParentType = gtk::ApplicationWindow; type Instance = subclass::simple::InstanceStruct<Self>; type Class = subclass::simple::ClassStruct<Self>; glib_object_subclass!(); fn new() -> Self { Self { widgets: OnceCell::new(), counter: Cell::new(0), } } } static MUSIC_FOLDER: &str = "musics"; impl ObjectImpl for SimpleWindowPrivate { glib_object_impl!(); // Here we are overriding the glib::Objcet::contructed // method. Its what gets called when we create our Object // and where we can initialize things. fn constructed(&self, obj: &glib::Object) { // ==== MUSIC SELCTOR BOX ===== let combo_box = gtk::ComboBoxTextBuilder::new() .width_request(50) .build(); let all_musics = std::fs::read_dir(MUSIC_FOLDER).unwrap().filter(|e| e.is_ok()).map(|e| e.unwrap()); all_musics.enumerate().for_each(|(idx, v)| { let name = v.path(); let name = name.to_string_lossy().replace(&format!("{}/", MUSIC_FOLDER), ""); println!("{}", name); combo_box.insert(idx as i32, None, &name); }); let combo_box = Arc::new(combo_box); // Audio player handle let audio_player = Arc::new(audio_handler::AudioHandler::new()); self.parent_constructed(obj); let self_ = obj.downcast_ref::<SimpleWindow>().unwrap(); // Basic UI elements let headerbar = gtk::HeaderBar::new(); let increment = gtk::Button::new_with_label("Add meaning to my life"); let reset = gtk::Button::new_with_label("Reset my life"); let no = gtk::Button::new_with_label("no"); let decrement = gtk::Button::new_with_label("Remove meaning from my life ;_;"); let label = gtk::Label::new(Some("What doth life has for you?")); let bbox = gtk::BoxBuilder::new() .orientation(gtk::Orientation::Vertical) .build(); let play_button = gtk::Button::new_with_label("Play"); let pause_button = gtk::Button::new_with_label("Pause"); let tbox = gtk::EntryBuilder::new() .height_request(10) .activates_default(true) .build(); tbox.set_text("I don't know what to do with that textbox DD:"); let test = Arc::new(tbox); let inner_tbox = test.clone(); test.clone().connect_activate(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); inner_tbox.set_text("WHy u pressed enter DDD:"); priv_.widgets.get().unwrap().label.set_text("WHy u pressed enter DDD:"); })); bbox.pack_start(test.as_ref(), false, false, 100); bbox.pack_start(&reset, false, false, 10); bbox.pack_start(&no, false, false, 10); bbox.pack_start(&label, false, false, 10); bbox.pack_start(&play_button, false, false, 10); bbox.pack_start(&pause_button, false, false, 10); bbox.pack_start(combo_box.as_ref(), false, false, 10); headerbar.set_title(Some("This is your life now")); headerbar.set_show_close_button(true); headerbar.pack_start(&increment); headerbar.pack_start(&decrement); let audio_player_clone = audio_player.clone(); let combo_box_clone = combo_box.clone(); // Music buttons closures play_button.connect_clicked(move |_| { let music = combo_box_clone.get_active_text().unwrap(); let music = format!("{}/{}", MUSIC_FOLDER, music.as_str()); audio_player_clone.play_music(music); }); let audio_player_clone = audio_player.clone(); pause_button.connect_clicked(move |_| { audio_player_clone.pause_music(); }); // Connect our method `on_increment_clicked` to be called // when the increment button is clicked. increment.connect_clicked(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); priv_.on_increment_clicked(); })); decrement.connect_clicked(clone!(@weak self_ => move |_| { let priv_ = SimpleWindowPrivate::from_instance(&self_); priv_.on_decrement_clicked(); })); reset.connect_clicked(clone!(@weak self_ => move |_| { println!("Maybe ;___;"); })); self_.add(&bbox); // self_.add(&label); self_.set_titlebar(Some(&headerbar)); self_.set_default_size(640, 480); self.widgets .set(WindowWidgets { headerbar, label, increment, decrement, reset, }) .expect("Failed to initialize window state"); } } impl SimpleWindowPrivate { fn
(&self) { self.counter.set(self.counter.get() + 1); let w = self.widgets.get().unwrap(); w.label .set_text(&format!("Your life has {} meaning", self.counter.get())); } fn on_decrement_clicked(&self) { self.counter.set(self.counter.get().wrapping_sub(1)); let w = self.widgets.get().unwrap(); w.label .set_text(&format!("Your life has {} meaning", self.counter.get())); } } impl WidgetImpl for SimpleWindowPrivate {} impl ContainerImpl for SimpleWindowPrivate {} impl BinImpl for SimpleWindowPrivate {} impl WindowImpl for SimpleWindowPrivate {} impl ApplicationWindowImpl for SimpleWindowPrivate {} glib_wrapper! { pub struct SimpleWindow( Object<subclass::simple::InstanceStruct<SimpleWindowPrivate>, subclass::simple::ClassStruct<SimpleWindowPrivate>, SimpleAppWindowClass>) @extends gtk::Widget, gtk::Container, gtk::Bin, gtk::Window, gtk::ApplicationWindow; match fn { get_type => || SimpleWindowPrivate::get_type().to_glib(), } } impl SimpleWindow { pub fn new(app: &gtk::Application) -> Self { glib::Object::new(Self::static_type(), &[("application", app)]) .expect("Failed to create SimpleWindow") .downcast::<SimpleWindow>() .expect("Created SimpleWindow is of wrong type") } } #[derive(Debug)] pub struct SimpleApplicationPrivate { window: OnceCell<SimpleWindow>, } impl ObjectSubclass for SimpleApplicationPrivate { const NAME: &'static str = "SimpleApplicationPrivate"; type ParentType = gtk::Application; type Instance = subclass::simple::InstanceStruct<Self>; type Class = subclass::simple::ClassStruct<Self>; glib_object_subclass!(); fn new() -> Self { Self { window: OnceCell::new(), } } } impl ObjectImpl for SimpleApplicationPrivate { glib_object_impl!(); } // When our application starts, the `startup` signal will be fired. // This gives us a chance to perform initialisation tasks that are not directly // related to showing a new window. After this, depending on how // the application is started, either `activate` or `open` will be called next. impl ApplicationImpl for SimpleApplicationPrivate { // `gio::Application::activate` is what gets called when the // application is launched by the desktop environment and // aksed to present itself. fn activate(&self, app: &gio::Application) { let app = app.downcast_ref::<gtk::Application>().unwrap(); let priv_ = SimpleApplicationPrivate::from_instance(app); let window = priv_ .window .get() .expect("Should always be initiliazed in gio_application_startup"); window.show_all(); window.present(); } // `gio::Application` is bit special. It does not get initialized // when `new` is called and the object created, but rather // once the `startup` signal is emitted and the `gio::Application::startup` // is called. // // Due to this, we create and initialize the `SimpleWindow` widget // here. Widgets can't be created before `startup` has been called. fn startup(&self, app: &gio::Application) { self.parent_startup(app); let app = app.downcast_ref
on_increment_clicked
identifier_name
configfunction.js
34\u503C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; }else{ var obj={attributeName:texts[0].split(":")[1],attributeCode:texts[1].split(":")[1],attributeType:texts[2].split(":")[1],lstConfigValueVO:[]}; jsonHead.push(obj); } }else{ msg+="\u6570\u636E\u503C\u533A\u57DF\u7B2C"+(i+1)+"\u5217\u9898\u5934\u503C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; } } } if(msg.length>0){ return msg; } var msgRowsValidate = ""; for(var i=1;i<rows;i++){ var defs; var def = table.rows[i].cells[0].getElementsByTagName("IMG")[1]; if(def.src.search("undefault")=="-1"){ defs ="1"; }else{ defs ="2"; } var flag = true; for(var j=1;j<=count;j++){ //取值 var td = table.rows[i].cells[j]; if(td){ var tempValues = td.getElementsByTagName("INPUT")[0].value; if(trimSpace(tempValues)!=""){ //校验数据值是否含有不允许的特殊字符 var validateDataRegexInfo = validateDataRegex(trimSpace(tempValues)); if(validateDataRegexInfo){ msg += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217"+validateDataRegexInfo; } flag = false; //只有值不为空时才进行验证 //验证输入的值是否合法 var attributeType = trimSpace(jsonHead[j-1].attributeType); var vInfo = validationData(attributeType,trimSpace(tempValues)); if(vInfo!=""){ //msg += "数据值区域第"+i+"行,第"+(j+1)+"列"+vInfo; msg += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217"+vInfo; } }else{ //msg += "数据值区域第"+i+"行,第"+(j+1)+"列值为空。<br>"; msgRowsValidate += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217\u503C\u4E3A\u7A7A\u3002<br>"; } var objValue = {configItemValue:trimSpace(tempValues),isDefaultValue:defs,rowNumber:i}; jsonHead[j-1].lstConfigValueVO.push(objValue); } } } //判断编码是否重复 msg+=attributeUnique(jsonHead); msg = msg + ":" + msgRowsValidate; return msg; } //验证单元格里面的特殊字符问题 function validateDataRegex(data){ var re=/\"/; if(data.match(re)){ //return "数据值不能包含"。<br>"; return "\u6570\u636E\u503C\u4E0D\u80FD\u5305\u542B\u0022\u3002<br>"; } } //div确定按钮事件 function confirmEvent(){ var valid = window.validater.validElement('AREA','#attr'); if(!valid[2]){ return ; } var temp = document.getElementById("temp").value; var attrName = cui('#attrName').getValue(); var attrCode = cui('#attrCode').getValue(); var attrType = cui('#attrType').getText(); var type = attrType; var msg = ''; if(trimSpace(attrName).length==0){ //msg+="属性名称不能为空<br>"; msg+="\u5C5E\u6027\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A<br>"; } if(trimSpace(attrCode).length==0){ //msg+="属性编码不能为空<br>"; msg+="\u5C5E\u6027\u7F16\u7801\u4E0D\u80FD\u4E3A\u7A7A<br>"; }else{ msg += isRepeat(attrCode,temp); } if(msg.length>0){ cui.alert(msg); return; } //var info = "名称:"+trimSpace(attrName)+",编码:"+trimSpace(attrCode)+",类型:"+type; var info = "\u540D\u79F0:"+trimSpace(attrName)+",\u7F16\u7801:"+trimSpace(attrCode)+",\u7C7B\u578B:"+type; document.getElementById("hiddenValue"+temp).value = info; document.getElementById("span"+temp).innerText=cutOutSide(trimSpace(attrName),9); document.getElementById("span"+temp).title=trimSpace(attrCode); document.getElementById("attr").style.display='none'; } //点击div上的确定时判断编码是否重复 function isRepeat(attrCode,index){ var table = document.getElementById("tdList"); var rows = table.rows.length; var columns = table.rows[0].cells.length; for(var i=1;i<=count;i++){ var hiddenObj = document.getElementById('hiddenValue'+i); if(hiddenObj){ var innerText = hiddenObj.value; if(innerText){ var texts = innerText.split(","); if(texts[0].split(":").length!=1){ if(i!=index){ if(trimSpace(attrCode)==trimSpace(texts[1].split(":")[1])){ //return "编码和第"+(i+1)+"列重复了"; return "\u7F16\u7801\u548C\u7B2C"+(i+1)+"\u5217\u91CD\u590D\u4E86"; } } } } } } return ""; } //显示div function showDiv(index,event){ var scroll = document.body.scrollLeft; var scrollY = document.body.scrollTop; document.getElementById("temp").value=index; var div = document.getElementById("attr"); div.style.display="block"; if(event.clientX<200){ div.style.left=(event.clientX+scroll) + 'px'; if(scrollY>0){ div.style.top=(event.clientY+scrollY) + 'px'; }else{ div.style.top=event.clientY + 'px'; } }else{ div.style.left=(event.clientX-200+scroll) + 'px'; if(scrollY>0){ div.style.top=(event.clientY+scrollY) + 'px'; }else{ div.style.top=event.clientY + 'px'; } } setSelectColumnValue(index); } //隐藏div function hideDiv(){ document.getElementById("attr").style.display = "none"; } //检验属性编码的唯一性 function attributeUnique(jsonValue){ for(var i=0;i<jsonValue.length;i++){ for(var j=0;j<jsonValue.length;j++){ if(i!=j){ if(jsonValue[i].attributeCode==jsonValue[j].attributeCode){ //return "第"+(i+3)+"列的属性编码和第"+(j+3)+"列的属性编码重复了<br>"; return "\u7B2C"+(i+3)+"\u5217\u7684\u5C5E\u6027\u7F16\u7801\u548C\u7B2C"+(j+3)+"\u5217\u7684\u5C5E\u6027\u7F16\u7801\u91CD\u590D\u4E86<br>"; } } } } return ""; } /** * 把值转换成json * @return */ function transValueToJson(){ var attrValues = document.getElementsByName("attrValue"); var json = []; for(var i=0,j=attrValues.length;i<j;i++){ var valuesInfo = attrValues[i].value.split("###"); var tempJson = {columnNum:valuesInfo[0],defaultValue:valuesInfo[1],rowNumber:valuesInfo[2],creatorId:valuesInfo[3],values:valuesInfo[4]}; json.push(tempJson); } return json; } function addBorder(obj){ obj.className = "input_mouseover"; } function remov
eBorder(obj){
identifier_name
configfunction.js
10)>30){ //return "值为'"+data+"'的日期类型数据"+parseInt(dateArray[1],10)+"月份天数不能超过30天。<br>"; return "\u503C\u4E3A'"+data+"'\u7684\u65E5\u671F\u7C7B\u578B\u6570\u636E"+parseInt(dateArray[1],10)+"\u6708\u4EFD\u5929\u6570\u4E0D\u80FD\u8D85\u8FC7\u0033\u0030\u5929\u3002<br>"; } }else{ if(parseInt(dateArray[2],10)>31){ //return "值为'"+data+"'的日期类型数据"+parseInt(dateArray[1],10)+"月份天数不能超过31天。<br>"; return "\u503C\u4E3A'"+data+"'\u7684\u65E5\u671F\u7C7B\u578B\u6570\u636E"+parseInt(dateArray[1],10)+"\u6708\u4EFD\u5929\u6570\u4E0D\u80FD\u8D85\u8FC7\u0033\u0031\u5929\u3002<br>"; } } if(parseInt(dateArray[2],10)==0){ //return "值为'"+data+"'的日期类型数据"+parseInt(dateArray[1],10)+"月份天数不能为0天。<br>"; return "\u503C\u4E3A'"+data+"'\u7684\u65E5\u671F\u7C7B\u578B\u6570\u636E"+parseInt(dateArray[1],10)+"\u6708\u4EFD\u5929\u6570\u4E0D\u80FD\u4E3A\u0030\u5929\u3002<br>"; } } //}else if(types=="4"||types=="布尔型"){ }else if(types=="4"||types=="\u5E03\u5C14\u578B"){ if(data!="true" && data!="false" && data!="TRUE" && data!="FALSE"){ //return "类型为布尔型的数据值必须为布尔型。如 true/false<br>"; return "\u7C7B\u578B\u4E3A\u5E03\u5C14\u578B\u7684\u6570\u636E\u503C\u5FC5\u987B\u4E3A\u5E03\u5C14\u578B\u3002\u5982 true/false<br>"; } } return ""; } /* *取值 */ function getValue(){ var jsonHead = []; //获取tabl对象 var table = document.getElementById("tdList"); var rows = table.rows.length; var columns = table.rows[0].cells.length; for(var i=1;i<=count;i++){ var hiddenObj = document.getElementById('hiddenValue'+i); if(hiddenObj){ var innerText = hiddenObj.value; var texts = innerText.replace(new RegExp("<br>|\n|\r","gm"),"").split(","); var obj={attributeName:texts[0].split(":")[1],attributeCode:texts[1].split(":")[1],attributeType:texts[2].split(":")[1],columnNumber:i,lstConfigValueVO:[]}; jsonHead.push(obj); } } for(var i=1;i<rows;i++){ var defs; var def = table.rows[i].cells[0].getElementsByTagName("IMG")[1]; if(def.src.search("undefault")=="-1"){ defs ="1"; }else{ defs ="2"; } var flag = true; for(var j=1;j<columns;j++){ var td = table.rows[i].cells[j]; if(td){ var values = td.getElementsByTagName("INPUT")[0]; var tValue = ""; if(values!=""){ tValue = values.value; } //获取属性值创建人 var creatorIdInputElement = td.getElementsByTagName("INPUT")[1]; var creatorId = ""; if(creatorIdInputElement!=""){ creatorId = creatorIdInputElement.value; if(!creatorId){ creatorId = globalUserId; } } var objValue = {configItemValue:tValue,isDefaultValue:defs,rowNumber:i,creatorId:creatorId}; //放入json对象中 jsonHead[j-1].lstConfigValueVO.push(objValue); } } } return JSON.stringify(jsonHead); } //获取基本类型的json值 function getBaseValue(formdata){ var jsonHead = []; var obj = {attributeName:formdata.configItemName,attributeCode:formdata.configItemFullCode,attributeType:'0',columnNumber:0,lstConfigValueVO:[]}; jsonHead.push(obj); var configValue = ''; if(formdata.configItemType == '3'){ configValue = formdata.dateConfigValue; }else if(formdata.configItemType == '4'){ configValue = formdata.booleanConfigValue; }else{ configValue = formdata.configItemValue; } var objValue = {configItemValue:configValue,isDefaultValue:2,rowNumber:0}; jsonHead[0].lstConfigValueVO.push(objValue); return JSON.stringify(jsonHead); } function validateValueField(){ var msg = ""; var jsonHead = []; //获取tabl对象 var table = document.getElementById("tdList"); var rows = table.rows.length; var columns = table.rows[0].cells.length; //至少保留一个属性列 if(columns<=1){ //msg+="数据值区域至少应保留一个可填属性的列。<br>"; msg+="\u6570\u636E\u503C\u533A\u57DF\u81F3\u5C11\u5E94\u4FDD\u7559\u4E00\u4E2A\u53EF\u586B\u5C5E\u6027\u7684\u5217\u3002<br>"; } if(rows<=1){ //msg+="数据值区域至少应保留一个可填值的行。<br>"; msg+="\u6570\u636E\u503C\u533A\u57DF\u81F3\u5C11\u5E94\u4FDD\u7559\u4E00\u4E2A\u53EF\u586B\u503C\u7684\u884C\u3002<br>"; } for(var i=1;i<=count;i++){ var hiddenObj = document.getElementById('hiddenValue'+i); if(hiddenObj){ var innerText = hiddenObj.value; if(innerText){ var texts = innerText.split(","); if(texts[0].split(":").length==1){ //msg+="数据值区域第"+(i+1)+"列题头值不能为空。<br>"; msg+="\u6570\u636E\u503C\u533A\u57DF\u7B2C"+(i+1)+"\u5217\u9898\u5934\u503C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; }else{ var obj={attributeName:texts[0].split(":")[1],attributeCode:texts[1].split(":")[1],attributeType:texts[2].split(":")[1],lstConfigValueVO:[]}; jsonHead.push(obj); } }else{ msg+="\u6570\u636E\u503C\u533A\u57DF\u7B2C"+(i+1)+"\u5217\u9898\u5934\u503C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; } } } if(msg.length>0){ return msg; } var msgRowsValidate = ""; for(var i=1;i<rows;i++){ var defs; var def = table.rows[i].cells[0].getElementsByTagName("IMG")[1]; if(def.src.search("undefault")=="-1"){ defs ="1"; }else{ defs ="2"; } var flag = true; for(var j=1;j<=count;j++){ //取值 var td = table.rows[i].cells[j];
if(td){
random_line_split
configfunction.js
\u6708\u4EFD\u5929\u6570\u4E0D\u80FD\u4E3A\u0030\u5929\u3002<br>"; } } //}else if(types=="4"||types=="布尔型"){ }else if(types=="4"||types=="\u5E03\u5C14\u578B"){ if(data!="true" && data!="false" && data!="TRUE" && data!="FALSE"){ //return "类型为布尔型的数据值必须为布尔型。如 true/false<br>"; return "\u7C7B\u578B\u4E3A\u5E03\u5C14\u578B\u7684\u6570\u636E\u503C\u5FC5\u987B\u4E3A\u5E03\u5C14\u578B\u3002\u5982 true/false<br>"; } } return ""; } /* *取值 */ function getValue(){ var jsonHead = []; //获取tabl对象 var table = document.getElementById("tdList"); var rows = table.rows.length; var columns = table.rows[0].cells.length; for(var i=1;i<=count;i++){ var hiddenObj = document.getElementById('hiddenValue'+i); if(hiddenObj){ var innerText = hiddenObj.value; var texts = innerText.replace(new RegExp("<br>|\n|\r","gm"),"").split(","); var obj={attributeName:texts[0].split(":")[1],attributeCode:texts[1].split(":")[1],attributeType:texts[2].split(":")[1],columnNumber:i,lstConfigValueVO:[]}; jsonHead.push(obj); } } for(var i=1;i<rows;i++){ var defs; var def = table.rows[i].cells[0].getElementsByTagName("IMG")[1]; if(def.src.search("undefault")=="-1"){ defs ="1"; }else{ defs ="2"; } var flag = true; for(var j=1;j<columns;j++){ var td = table.rows[i].cells[j]; if(td){ var values = td.getElementsByTagName("INPUT")[0]; var tValue = ""; if(values!=""){ tValue = values.value; } //获取属性值创建人 var creatorIdInputElement = td.getElementsByTagName("INPUT")[1]; var creatorId = ""; if(creatorIdInputElement!=""){ creatorId = creatorIdInputElement.value; if(!creatorId){ creatorId = globalUserId; } } var objValue = {configItemValue:tValue,isDefaultValue:defs,rowNumber:i,creatorId:creatorId}; //放入json对象中 jsonHead[j-1].lstConfigValueVO.push(objValue); } } } return JSON.stringify(jsonHead); } //获取基本类型的json值 function getBaseValue(formdata){ var jsonHead = []; var obj = {attributeName:formdata.configItemName,attributeCode:formdata.configItemFullCode,attributeType:'0',columnNumber:0,lstConfigValueVO:[]}; jsonHead.push(obj); var configValue = ''; if(formdata.configItemType == '3'){ configValue = formdata.dateConfigValue; }else if(formdata.configItemType == '4'){ configValue = formdata.booleanConfigValue; }else{ configValue = formdata.configItemValue; } var objValue = {configItemValue:configValue,isDefaultValue:2,rowNumber:0}; jsonHead[0].lstConfigValueVO.push(objValue); return JSON.stringify(jsonHead); } function validateValueField(){ var msg = ""; var jsonHead = []; //获取tabl对象 var table = document.getElementById("tdList"); var rows = table.rows.length; var columns = table.rows[0].cells.length; //至少保留一个属性列 if(columns<=1){ //msg+="数据值区域至少应保留一个可填属性的列。<br>"; msg+="\u6570\u636E\u503C\u533A\u57DF\u81F3\u5C11\u5E94\u4FDD\u7559\u4E00\u4E2A\u53EF\u586B\u5C5E\u6027\u7684\u5217\u3002<br>"; } if(rows<=1){ //msg+="数据值区域至少应保留一个可填值的行。<br>"; msg+="\u6570\u636E\u503C\u533A\u57DF\u81F3\u5C11\u5E94\u4FDD\u7559\u4E00\u4E2A\u53EF\u586B\u503C\u7684\u884C\u3002<br>"; } for(var i=1;i<=count;i++){ var hiddenObj = document.getElementById('hiddenValue'+i); if(hiddenObj){ var innerText = hiddenObj.value; if(innerText){ var texts = innerText.split(","); if(texts[0].split(":").length==1){ //msg+="数据值区域第"+(i+1)+"列题头值不能为空。<br>"; msg+="\u6570\u636E\u503C\u533A\u57DF\u7B2C"+(i+1)+"\u5217\u9898\u5934\u503C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; }else{ var obj={attributeName:texts[0].split(":")[1],attributeCode:texts[1].split(":")[1],attributeType:texts[2].split(":")[1],lstConfigValueVO:[]}; jsonHead.push(obj); } }else{ msg+="\u6570\u636E\u503C\u533A\u57DF\u7B2C"+(i+1)+"\u5217\u9898\u5934\u503C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; } } } if(msg.length>0){ return msg; } var msgRowsValidate = ""; for(var i=1;i<rows;i++){ var defs; var def = table.rows[i].cells[0].getElementsByTagName("IMG")[1]; if(def.src.search("undefault")=="-1"){ defs ="1"; }else{ defs ="2"; } var flag = true; for(var j=1;j<=count;j++){ //取值 var td = table.rows[i].cells[j]; if(td){ var tempValues = td.getElementsByTagName("INPUT")[0].value; if(trimSpace(tempValues)!=""){ //校验数据值是否含有不允许的特殊字符 var validateDataRegexInfo = validateDataRegex(trimSpace(tempValues)); if(validateDataRegexInfo){ msg += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217"+validat
eDataRegexInfo; } flag = false; //只有值不为空时才进行验证 //验证输入的值是否合法 var attributeType = trimSpace(jsonHead[j-1].attributeType); var vInfo = validationData(attributeType,trimSpace(tempValues)); if(vInfo!=""){ //msg += "数据值区域第"+i+"行,第"+(j+1)+"列"+vInfo; msg += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217"+vInfo; } }else{ //msg += "数据值区域第"+i+"行,第"+(j+1)+"列值为空。<br>"; msgRowsValidate += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217\u503C\u4E3A\u7A7A\u3002<br>"; } var objValue = {configItemValue:trimSpace(tempValues),isDefaultValue:defs,rowNumber:i}; jsonHead[j-1].lstConfigValueVO.push(objValue); } } }
conditional_block
configfunction.js
C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; }else{ var obj={attributeName:texts[0].split(":")[1],attributeCode:texts[1].split(":")[1],attributeType:texts[2].split(":")[1],lstConfigValueVO:[]}; jsonHead.push(obj); } }else{ msg+="\u6570\u636E\u503C\u533A\u57DF\u7B2C"+(i+1)+"\u5217\u9898\u5934\u503C\u4E0D\u80FD\u4E3A\u7A7A\u3002<br>"; } } } if(msg.length>0){ return msg; } var msgRowsValidate = ""; for(var i=1;i<rows;i++){ var defs; var def = table.rows[i].cells[0].getElementsByTagName("IMG")[1]; if(def.src.search("undefault")=="-1"){ defs ="1"; }else{ defs ="2"; } var flag = true; for(var j=1;j<=count;j++){ //取值 var td = table.rows[i].cells[j]; if(td){ var tempValues = td.getElementsByTagName("INPUT")[0].value; if(trimSpace(tempValues)!=""){ //校验数据值是否含有不允许的特殊字符 var validateDataRegexInfo = validateDataRegex(trimSpace(tempValues)); if(validateDataRegexInfo){ msg += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217"+validateDataRegexInfo; } flag = false; //只有值不为空时才进行验证 //验证输入的值是否合法 var attributeType = trimSpace(jsonHead[j-1].attributeType); var vInfo = validationData(attributeType,trimSpace(tempValues)); if(vInfo!=""){ //msg += "数据值区域第"+i+"行,第"+(j+1)+"列"+vInfo; msg += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217"+vInfo; } }else{ //msg += "数据值区域第"+i+"行,第"+(j+1)+"列值为空。<br>"; msgRowsValidate += "\u6570\u636E\u503C\u533A\u57DF\u7B2C"+i+"\u884C\u002C\u7B2C"+(j+1)+"\u5217\u503C\u4E3A\u7A7A\u3002<br>"; } var objValue = {configItemValue:trimSpace(tempValues),isDefaultValue:defs,rowNumber:i}; jsonHead[j-1].lstConfigValueVO.push(objValue); } } } //判断编码是否重复 msg+=attributeUnique(jsonHead); msg = msg + ":" + msgRowsValidate; return msg; } //验证单元格里面的特殊字符问题 function validateDataRegex(data){ var re=/\"/; if(data.match(re)){ //return "数据值不能包含"。<br>"; return "\u6570\u636E\u503C\u4E0D\u80FD\u5305\u542B\u0022\u3002<br>"; } } //div确定按钮事件 function confirmEvent(){ var valid = window.validater.validElement('AREA','#attr'); if(!valid[2]){ return ; } var temp = document.getElementById("temp").value; var attrName = cui('#attrName').getValue(); var attrCode = cui('#attrCode').getValue(); var attrType = cui('#attrType').getText(); var type = attrType; var msg = ''; if(trimSpace(attrName).length==0){ //msg+="属性名称不能为空<br>"; msg+="\u5C5E\u6027\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A<br>"; } if(trimSpace(attrCode).length==0){ //msg+="属性编码不能为空<br>"; msg+="\u5C5E\u6027\u7F16\u7801\u4E0D\u80FD\u4E3A\u7A7A<br>"; }else{ msg += isRepeat(attrCode,temp); } if(msg.length>0){ cui.alert(msg); return; } //var info = "名称:"+trimSpace(attrName)+",编码:"+trimSpace(attrCode)+",类型:"+type; var info = "\u540D\u79F0:"+trimSpace(attrName)+",\u7F16\u7801:"+trimSpace(attrCode)+",\u7C7B\u578B:"+type; document.getElementById("hiddenValue"+temp).value = info; document.getElementById("span"+temp).innerText=cutOutSide(trimSpace(attrName),9); document.getElementById("span"+temp).title=trimSpace(attrCode); document.getElementById("attr").style.display='none'; } //点击div上的确定时判断编码是否重复 function isRepeat(attrCode,index){ var table = document.getElementById("tdList"); var rows = table.rows.length; var columns = table.rows[0].cells.length; for(var i=1;i<=count;i++){ var hiddenObj = document.getElementById('hiddenValue'+i); if(hiddenObj){ var innerText = hiddenObj.value; if(innerText){ var texts = innerText.split(","); if(texts[0].split(":").length!=1){ if(i!=index){ if(trimSpace(attrCode)==trimSpace(texts[1].split(":")[1])){ //return "编码和第"+(i+1)+"列重复了"; return "\u7F16\u7801\u548C\u7B2C"+(i+1)+"\u5217\u91CD\u590D\u4E86"; } } } } } } return ""; } //显示div function showDiv(index,event){ var scroll = document.body.scrollLeft; var scrollY = document.body.scrollTop; document.getElementById("temp").value=index; var div = document.getElementById("attr"); div.style.display="block"; if(event.clientX<200){ div.style.left=(event.clientX+scroll) + 'px'; if(scrollY>0){ div.style.top=(event.clientY+scrollY) + 'px'; }else{ div.style.top=event.clientY + 'px'; } }else{ div.style.left=(event.clientX-200+scroll) + 'px'; if(scrollY>0){ div.style.top=(event.clientY+scrollY) + 'px'; }else{ div.style.top=event.clientY + 'px'; } } setSelectColumnValue(index); } //隐藏div function hideDiv(){ document.getElementById("attr").style.display = "none"; } //检验属性编码的唯一性 function attributeUnique(jsonValue){ for(var i=0;i<jsonValue.length;i++){ for(var j=0;j<jsonValue.length;j++){ if(i!=j){ if(jsonValue[i].attributeCode==jsonValue[j].attributeCode){ //return "第"+(i+3)+"列的属性编码和第"+(j+3)+"列的属性编码重复了<br>"; return "\u7B2C"+(i+3)+"\u5217\u7684\u5C5E\u6027\u7F16\u7801\u548C\u7B2C"+(j+3)+"\u5217\u7684\u5C5E\u6027\u7F16\u7801\u91CD\u590D\u4E86<br>"; } } } } return ""; } /** * 把值转换成json * @return */ function transValueToJson(){ var attrValues = document.getElementsByName("attrValue"); var json = []; for(var i=0,j=attrValues.length;i<j;i++){ var valuesInfo = attrValues[i].value.split("###"); var tempJson = {columnNum:valuesInfo[0],defaultValue:valuesInfo[1],rowNumber:valuesInfo[2],creatorId:valuesInfo[3],values:valuesInfo[4]}; json.push(tempJson); } return json; } function addBorder(obj){ obj.className = "input_mouseover"; } function removeBorder(obj){ obj.className = "input_mouseout"; }
identifier_body
lstm.py
00000) # df.head() # print(df.shape) df_filtered=df[df['stars'] !=3] # print(df_filtered.shape) #print(df_filtered.describe().T) text=list(df_filtered['text']) stars=list(df_filtered['stars']) print(type(text)) label=[] for item in stars: if item>= 4: y=1 else: y=0 label.append(y) label=np.array(label) #we can get punctuation from string library from string import punctuation print(punctuation) all_reviews=[] for item in text: item = item.lower() item = "".join([ch for ch in item if ch not in punctuation]) all_reviews.append(item) all_text = " ".join(all_reviews) print(all_text[0:20]) all_words = all_text.split() print(all_words[0:10]) from collections import Counter # Count all the words using Counter Method count_words = Counter(all_words) total_words=len(all_words) sorted_words=count_words.most_common(total_words) #print(sorted_words[:30]) vocab_to_int={w:i+1 for i,(w,c) in enumerate(sorted_words)} #print(vocab_to_int) #reviews_ints = [] #for review in all_words: #reviews_ints.append([vocab_to_int[word] for word in all_words]) encoded_reviews=list() for review in all_reviews: encoded_review=list() for word in review.split(): if word not in vocab_to_int.keys(): #if word is not available in vocab_to_int put 0 in that place encoded_review.append(0) else: encoded_review.append(vocab_to_int[word]) if len(encoded_review) == 0: encoded_reviews.append([0]) else: encoded_reviews.append(encoded_review) reviews_len = [len(x) for x in encoded_reviews] pd.Series(reviews_len).hist() plt.xlabel('Words') plt.ylabel('Count') plt.show() # stats about vocabulary #print('Unique words: ', len((vocab_to_int))) #print() # print tokens in first review #print('Tokenized review: \n', encoded_reviews[:1]) def pad_features(reviews_ints, seq_length): ''' Return features of review_ints, where each review is padded with 0's or truncated to the input seq_length. ''' ## getting the correct rows x cols shape features = np.zeros((len(encoded_reviews), seq_length), dtype=int) ## for each review, I grab that review for i, row in enumerate(encoded_reviews): features[i, -len(row):] = np.array(row)[:seq_length] return features seq_length = 200 features = pad_features(encoded_reviews, seq_length=seq_length) ## test statements - do not change - ## assert len(features)==len(encoded_reviews), "Your features should have as many rows as reviews." assert len(features[0])==seq_length, "Each feature row should contain seq_length values." # print first 10 values of the first 30 batches #print(features[:30,:10]) train_x, test_x, train_y, test_y = model_selection.train_test_split(features,label, test_size=0.2, random_state=42) ## print out the shapes of your resultant feature data print("\t\t\tFeatures Shapes:") print("Train set: \t\t{}".format(train_x.shape), "\nTest set: \t\t{}".format(test_x.shape)) #train_x=np.array(train_x).astype('float') #train_y=np.array(train_x).astype('float') #test_x=np.array(train_x).astype('float') #test_y=np.array(train_x).astype('float') import torch from torch.utils.data import TensorDataset, DataLoader # create Tensor datasets train_data = TensorDataset(torch.from_numpy(train_x), torch.from_numpy(train_y)) test_data = TensorDataset(torch.from_numpy(test_x), torch.from_numpy(test_y)) # dataloaders batch_size = 128 # make sure to SHUFFLE your data train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size) test_loader = DataLoader(test_data, shuffle=True, batch_size=batch_size) dataiter = iter(train_loader) sample_x, sample_y = dataiter.next() # print('Sample input size: ', sample_x.size()) # batch_size, seq_length # print('Sample input: \n', sample_x) # print() # print('Sample label size: ', sample_y.size()) # batch_size # print('Sample label: \n', sample_y) # First checking if GPU is available train_on_gpu=torch.cuda.is_available() if(train_on_gpu): print('Training on GPU.') else: print('No GPU available, training on CPU.') import torch.nn as nn class SentimentRNN(nn.Module): """ The RNN model that will be used to perform Sentiment analysis. """ def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, drop_prob=0.5): """ Initialize the model by setting up the layers. """ super(SentimentRNN, self).__init__() self.output_size = output_size self.n_layers = n_layers self.hidden_dim = hidden_dim # embedding and LSTM layers self.embedding = nn.Embedding(vocab_size, embedding_dim) self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout=drop_prob, batch_first=True) # dropout layer self.dropout = nn.Dropout(0.3) # linear and sigmoid layer self.fc = nn.Linear(hidden_dim, output_size) self.sig = nn.Sigmoid() def
(self, x, hidden): """ Perform a forward pass of our model on some input and hidden state. """ batch_size = x.size(0) # embeddings and lstm_out embeds = self.embedding(x) lstm_out, hidden = self.lstm(embeds, hidden) # stack up lstm outputs lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim) # dropout and fully connected layer out = self.dropout(lstm_out) out = self.fc(out) # sigmoid function sig_out = self.sig(out) # reshape to be batch_size first sig_out = sig_out.view(batch_size, -1) sig_out = sig_out[:, -1] # get last batch of labels # return last sigmoid output and hidden state return sig_out, hidden def init_hidden(self, batch_size, train_on_gpu): ''' Initializes hidden state ''' # Create two new tensors with sizes n_layers x batch_size x hidden_dim, # initialized to zero, for hidden state and cell state of LSTM weight = next(self.parameters()).data if(train_on_gpu): hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda()) else: hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_()) return hidden # Instantiate the model w/ hyperparams vocab_size = len(vocab_to_int) + 1 # +1 for zero padding + our word tokens output_size = 1 embedding_dim = 400 hidden_dim = 256 n_layers = 2 net = SentimentRNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers) print(net) # loss and optimization functions lr=0.001 criterion = nn.BCELoss() optimizer = torch.optim.Adam(net.parameters(), lr=lr) # training params epochs = 4 counter = 0 print_every = 100 clip=5 # gradient clipping train_on_gpu = True # move model to GPU, if available if(train_on_gpu): net.cuda() net.train() # train for some number of epochs for e in range(epochs): # initialize hidden state h = net.init_hidden(batch_size, train_on_gpu) counter = 0 # batch loop for inputs, labels in train_loader: counter += 1 #print('epoce: {e}, batch: {b}'.format(e=e, b=counter)) if (labels.shape[0] != batch_size): continue inputs = inputs.type(torch.LongTensor) labels = labels.type(torch.LongTensor) if(train_on_gpu): inputs, labels = inputs.cuda(), labels.cuda() # Creating new variables for the hidden state, otherwise # we'd backprop through the entire training history h = tuple([each.data for each in h]) # zero accumulated gradients net.zero_grad() output, h = net(inputs, h) # calculate the loss and perform backprop loss = criterion(output.squeeze(), labels.float()) loss.backward() # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs. nn.utils.clip_grad_norm_(net.parameters(), clip) optimizer.step() # Get test data loss and accuracy # = [] # track loss num_correct = 0 # init hidden state h = net.init_hidden(batch_size, train_on_gpu) counter=0 net.eval() all_prediction = [] # iterate over test data for inputs, labels in
forward
identifier_name
lstm.py
100000) # df.head() # print(df.shape) df_filtered=df[df['stars'] !=3] # print(df_filtered.shape) #print(df_filtered.describe().T) text=list(df_filtered['text']) stars=list(df_filtered['stars']) print(type(text)) label=[] for item in stars: if item>= 4: y=1 else: y=0 label.append(y) label=np.array(label) #we can get punctuation from string library from string import punctuation print(punctuation) all_reviews=[] for item in text: item = item.lower() item = "".join([ch for ch in item if ch not in punctuation]) all_reviews.append(item) all_text = " ".join(all_reviews) print(all_text[0:20]) all_words = all_text.split() print(all_words[0:10]) from collections import Counter # Count all the words using Counter Method count_words = Counter(all_words) total_words=len(all_words) sorted_words=count_words.most_common(total_words) #print(sorted_words[:30]) vocab_to_int={w:i+1 for i,(w,c) in enumerate(sorted_words)} #print(vocab_to_int) #reviews_ints = [] #for review in all_words: #reviews_ints.append([vocab_to_int[word] for word in all_words]) encoded_reviews=list() for review in all_reviews: encoded_review=list() for word in review.split(): if word not in vocab_to_int.keys(): #if word is not available in vocab_to_int put 0 in that place encoded_review.append(0) else: encoded_review.append(vocab_to_int[word]) if len(encoded_review) == 0: encoded_reviews.append([0]) else: encoded_reviews.append(encoded_review) reviews_len = [len(x) for x in encoded_reviews] pd.Series(reviews_len).hist() plt.xlabel('Words') plt.ylabel('Count') plt.show() # stats about vocabulary #print('Unique words: ', len((vocab_to_int))) #print() # print tokens in first review #print('Tokenized review: \n', encoded_reviews[:1]) def pad_features(reviews_ints, seq_length): ''' Return features of review_ints, where each review is padded with 0's or truncated to the input seq_length. ''' ## getting the correct rows x cols shape features = np.zeros((len(encoded_reviews), seq_length), dtype=int) ## for each review, I grab that review for i, row in enumerate(encoded_reviews): features[i, -len(row):] = np.array(row)[:seq_length] return features seq_length = 200 features = pad_features(encoded_reviews, seq_length=seq_length) ## test statements - do not change - ## assert len(features)==len(encoded_reviews), "Your features should have as many rows as reviews." assert len(features[0])==seq_length, "Each feature row should contain seq_length values." # print first 10 values of the first 30 batches #print(features[:30,:10]) train_x, test_x, train_y, test_y = model_selection.train_test_split(features,label, test_size=0.2, random_state=42) ## print out the shapes of your resultant feature data print("\t\t\tFeatures Shapes:") print("Train set: \t\t{}".format(train_x.shape), "\nTest set: \t\t{}".format(test_x.shape)) #train_x=np.array(train_x).astype('float') #train_y=np.array(train_x).astype('float') #test_x=np.array(train_x).astype('float') #test_y=np.array(train_x).astype('float') import torch from torch.utils.data import TensorDataset, DataLoader # create Tensor datasets train_data = TensorDataset(torch.from_numpy(train_x), torch.from_numpy(train_y)) test_data = TensorDataset(torch.from_numpy(test_x), torch.from_numpy(test_y)) # dataloaders batch_size = 128 # make sure to SHUFFLE your data train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size) test_loader = DataLoader(test_data, shuffle=True, batch_size=batch_size) dataiter = iter(train_loader) sample_x, sample_y = dataiter.next() # print('Sample input size: ', sample_x.size()) # batch_size, seq_length # print('Sample input: \n', sample_x) # print() # print('Sample label size: ', sample_y.size()) # batch_size # print('Sample label: \n', sample_y) # First checking if GPU is available train_on_gpu=torch.cuda.is_available() if(train_on_gpu): print('Training on GPU.') else: print('No GPU available, training on CPU.') import torch.nn as nn class SentimentRNN(nn.Module): """ The RNN model that will be used to perform Sentiment analysis. """ def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, drop_prob=0.5): """ Initialize the model by setting up the layers. """ super(SentimentRNN, self).__init__() self.output_size = output_size self.n_layers = n_layers self.hidden_dim = hidden_dim # embedding and LSTM layers self.embedding = nn.Embedding(vocab_size, embedding_dim) self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout=drop_prob, batch_first=True) # dropout layer self.dropout = nn.Dropout(0.3) # linear and sigmoid layer self.fc = nn.Linear(hidden_dim, output_size) self.sig = nn.Sigmoid() def forward(self, x, hidden): """ Perform a forward pass of our model on some input and hidden state. """ batch_size = x.size(0) # embeddings and lstm_out embeds = self.embedding(x) lstm_out, hidden = self.lstm(embeds, hidden) # stack up lstm outputs lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim) # dropout and fully connected layer out = self.dropout(lstm_out) out = self.fc(out) # sigmoid function sig_out = self.sig(out) # reshape to be batch_size first sig_out = sig_out.view(batch_size, -1) sig_out = sig_out[:, -1] # get last batch of labels # return last sigmoid output and hidden state return sig_out, hidden def init_hidden(self, batch_size, train_on_gpu): ''' Initializes hidden state ''' # Create two new tensors with sizes n_layers x batch_size x hidden_dim, # initialized to zero, for hidden state and cell state of LSTM weight = next(self.parameters()).data if(train_on_gpu): hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda()) else: hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_()) return hidden # Instantiate the model w/ hyperparams vocab_size = len(vocab_to_int) + 1 # +1 for zero padding + our word tokens output_size = 1 embedding_dim = 400 hidden_dim = 256 n_layers = 2 net = SentimentRNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers) print(net) # loss and optimization functions lr=0.001 criterion = nn.BCELoss() optimizer = torch.optim.Adam(net.parameters(), lr=lr) # training params epochs = 4 counter = 0 print_every = 100 clip=5 # gradient clipping train_on_gpu = True # move model to GPU, if available if(train_on_gpu): net.cuda() net.train() # train for some number of epochs for e in range(epochs): # initialize hidden state
output, h = net(inputs, h) # calculate the loss and perform backprop loss = criterion(output.squeeze(), labels.float()) loss.backward() # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs. nn.utils.clip_grad_norm_(net.parameters(), clip) optimizer.step() # Get test data loss and accuracy # = [] # track loss num_correct = 0 # init hidden state h = net.init_hidden(batch_size, train_on_gpu) counter=0 net.eval() all_prediction = [] # iterate over test data for inputs, labels in
h = net.init_hidden(batch_size, train_on_gpu) counter = 0 # batch loop for inputs, labels in train_loader: counter += 1 #print('epoce: {e}, batch: {b}'.format(e=e, b=counter)) if (labels.shape[0] != batch_size): continue inputs = inputs.type(torch.LongTensor) labels = labels.type(torch.LongTensor) if(train_on_gpu): inputs, labels = inputs.cuda(), labels.cuda() # Creating new variables for the hidden state, otherwise # we'd backprop through the entire training history h = tuple([each.data for each in h]) # zero accumulated gradients net.zero_grad()
conditional_block
lstm.py
00000) # df.head() # print(df.shape) df_filtered=df[df['stars'] !=3] # print(df_filtered.shape) #print(df_filtered.describe().T) text=list(df_filtered['text']) stars=list(df_filtered['stars']) print(type(text)) label=[] for item in stars: if item>= 4: y=1 else: y=0 label.append(y) label=np.array(label) #we can get punctuation from string library from string import punctuation print(punctuation) all_reviews=[] for item in text: item = item.lower() item = "".join([ch for ch in item if ch not in punctuation]) all_reviews.append(item) all_text = " ".join(all_reviews) print(all_text[0:20]) all_words = all_text.split() print(all_words[0:10]) from collections import Counter # Count all the words using Counter Method count_words = Counter(all_words) total_words=len(all_words) sorted_words=count_words.most_common(total_words) #print(sorted_words[:30]) vocab_to_int={w:i+1 for i,(w,c) in enumerate(sorted_words)} #print(vocab_to_int) #reviews_ints = [] #for review in all_words: #reviews_ints.append([vocab_to_int[word] for word in all_words]) encoded_reviews=list() for review in all_reviews: encoded_review=list() for word in review.split(): if word not in vocab_to_int.keys(): #if word is not available in vocab_to_int put 0 in that place encoded_review.append(0) else: encoded_review.append(vocab_to_int[word]) if len(encoded_review) == 0: encoded_reviews.append([0]) else: encoded_reviews.append(encoded_review) reviews_len = [len(x) for x in encoded_reviews] pd.Series(reviews_len).hist() plt.xlabel('Words') plt.ylabel('Count') plt.show() # stats about vocabulary #print('Unique words: ', len((vocab_to_int))) #print() # print tokens in first review #print('Tokenized review: \n', encoded_reviews[:1]) def pad_features(reviews_ints, seq_length): ''' Return features of review_ints, where each review is padded with 0's or truncated to the input seq_length. ''' ## getting the correct rows x cols shape features = np.zeros((len(encoded_reviews), seq_length), dtype=int) ## for each review, I grab that review for i, row in enumerate(encoded_reviews): features[i, -len(row):] = np.array(row)[:seq_length] return features seq_length = 200 features = pad_features(encoded_reviews, seq_length=seq_length) ## test statements - do not change - ## assert len(features)==len(encoded_reviews), "Your features should have as many rows as reviews." assert len(features[0])==seq_length, "Each feature row should contain seq_length values." # print first 10 values of the first 30 batches #print(features[:30,:10]) train_x, test_x, train_y, test_y = model_selection.train_test_split(features,label, test_size=0.2, random_state=42) ## print out the shapes of your resultant feature data print("\t\t\tFeatures Shapes:") print("Train set: \t\t{}".format(train_x.shape), "\nTest set: \t\t{}".format(test_x.shape)) #train_x=np.array(train_x).astype('float') #train_y=np.array(train_x).astype('float') #test_x=np.array(train_x).astype('float') #test_y=np.array(train_x).astype('float') import torch from torch.utils.data import TensorDataset, DataLoader # create Tensor datasets train_data = TensorDataset(torch.from_numpy(train_x), torch.from_numpy(train_y)) test_data = TensorDataset(torch.from_numpy(test_x), torch.from_numpy(test_y)) # dataloaders batch_size = 128 # make sure to SHUFFLE your data train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size) test_loader = DataLoader(test_data, shuffle=True, batch_size=batch_size) dataiter = iter(train_loader) sample_x, sample_y = dataiter.next() # print('Sample input size: ', sample_x.size()) # batch_size, seq_length # print('Sample input: \n', sample_x) # print() # print('Sample label size: ', sample_y.size()) # batch_size # print('Sample label: \n', sample_y) # First checking if GPU is available train_on_gpu=torch.cuda.is_available() if(train_on_gpu): print('Training on GPU.') else: print('No GPU available, training on CPU.') import torch.nn as nn class SentimentRNN(nn.Module): """ The RNN model that will be used to perform Sentiment analysis. """ def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, drop_prob=0.5):
def forward(self, x, hidden): """ Perform a forward pass of our model on some input and hidden state. """ batch_size = x.size(0) # embeddings and lstm_out embeds = self.embedding(x) lstm_out, hidden = self.lstm(embeds, hidden) # stack up lstm outputs lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim) # dropout and fully connected layer out = self.dropout(lstm_out) out = self.fc(out) # sigmoid function sig_out = self.sig(out) # reshape to be batch_size first sig_out = sig_out.view(batch_size, -1) sig_out = sig_out[:, -1] # get last batch of labels # return last sigmoid output and hidden state return sig_out, hidden def init_hidden(self, batch_size, train_on_gpu): ''' Initializes hidden state ''' # Create two new tensors with sizes n_layers x batch_size x hidden_dim, # initialized to zero, for hidden state and cell state of LSTM weight = next(self.parameters()).data if(train_on_gpu): hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda()) else: hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_()) return hidden # Instantiate the model w/ hyperparams vocab_size = len(vocab_to_int) + 1 # +1 for zero padding + our word tokens output_size = 1 embedding_dim = 400 hidden_dim = 256 n_layers = 2 net = SentimentRNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers) print(net) # loss and optimization functions lr=0.001 criterion = nn.BCELoss() optimizer = torch.optim.Adam(net.parameters(), lr=lr) # training params epochs = 4 counter = 0 print_every = 100 clip=5 # gradient clipping train_on_gpu = True # move model to GPU, if available if(train_on_gpu): net.cuda() net.train() # train for some number of epochs for e in range(epochs): # initialize hidden state h = net.init_hidden(batch_size, train_on_gpu) counter = 0 # batch loop for inputs, labels in train_loader: counter += 1 #print('epoce: {e}, batch: {b}'.format(e=e, b=counter)) if (labels.shape[0] != batch_size): continue inputs = inputs.type(torch.LongTensor) labels = labels.type(torch.LongTensor) if(train_on_gpu): inputs, labels = inputs.cuda(), labels.cuda() # Creating new variables for the hidden state, otherwise # we'd backprop through the entire training history h = tuple([each.data for each in h]) # zero accumulated gradients net.zero_grad() output, h = net(inputs, h) # calculate the loss and perform backprop loss = criterion(output.squeeze(), labels.float()) loss.backward() # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs. nn.utils.clip_grad_norm_(net.parameters(), clip) optimizer.step() # Get test data loss and accuracy # = [] # track loss num_correct = 0 # init hidden state h = net.init_hidden(batch_size, train_on_gpu) counter=0 net.eval() all_prediction = [] # iterate over test data for inputs, labels in
""" Initialize the model by setting up the layers. """ super(SentimentRNN, self).__init__() self.output_size = output_size self.n_layers = n_layers self.hidden_dim = hidden_dim # embedding and LSTM layers self.embedding = nn.Embedding(vocab_size, embedding_dim) self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout=drop_prob, batch_first=True) # dropout layer self.dropout = nn.Dropout(0.3) # linear and sigmoid layer self.fc = nn.Linear(hidden_dim, output_size) self.sig = nn.Sigmoid()
identifier_body
lstm.py
00000) # df.head() # print(df.shape) df_filtered=df[df['stars'] !=3] # print(df_filtered.shape) #print(df_filtered.describe().T)
print(type(text)) label=[] for item in stars: if item>= 4: y=1 else: y=0 label.append(y) label=np.array(label) #we can get punctuation from string library from string import punctuation print(punctuation) all_reviews=[] for item in text: item = item.lower() item = "".join([ch for ch in item if ch not in punctuation]) all_reviews.append(item) all_text = " ".join(all_reviews) print(all_text[0:20]) all_words = all_text.split() print(all_words[0:10]) from collections import Counter # Count all the words using Counter Method count_words = Counter(all_words) total_words=len(all_words) sorted_words=count_words.most_common(total_words) #print(sorted_words[:30]) vocab_to_int={w:i+1 for i,(w,c) in enumerate(sorted_words)} #print(vocab_to_int) #reviews_ints = [] #for review in all_words: #reviews_ints.append([vocab_to_int[word] for word in all_words]) encoded_reviews=list() for review in all_reviews: encoded_review=list() for word in review.split(): if word not in vocab_to_int.keys(): #if word is not available in vocab_to_int put 0 in that place encoded_review.append(0) else: encoded_review.append(vocab_to_int[word]) if len(encoded_review) == 0: encoded_reviews.append([0]) else: encoded_reviews.append(encoded_review) reviews_len = [len(x) for x in encoded_reviews] pd.Series(reviews_len).hist() plt.xlabel('Words') plt.ylabel('Count') plt.show() # stats about vocabulary #print('Unique words: ', len((vocab_to_int))) #print() # print tokens in first review #print('Tokenized review: \n', encoded_reviews[:1]) def pad_features(reviews_ints, seq_length): ''' Return features of review_ints, where each review is padded with 0's or truncated to the input seq_length. ''' ## getting the correct rows x cols shape features = np.zeros((len(encoded_reviews), seq_length), dtype=int) ## for each review, I grab that review for i, row in enumerate(encoded_reviews): features[i, -len(row):] = np.array(row)[:seq_length] return features seq_length = 200 features = pad_features(encoded_reviews, seq_length=seq_length) ## test statements - do not change - ## assert len(features)==len(encoded_reviews), "Your features should have as many rows as reviews." assert len(features[0])==seq_length, "Each feature row should contain seq_length values." # print first 10 values of the first 30 batches #print(features[:30,:10]) train_x, test_x, train_y, test_y = model_selection.train_test_split(features,label, test_size=0.2, random_state=42) ## print out the shapes of your resultant feature data print("\t\t\tFeatures Shapes:") print("Train set: \t\t{}".format(train_x.shape), "\nTest set: \t\t{}".format(test_x.shape)) #train_x=np.array(train_x).astype('float') #train_y=np.array(train_x).astype('float') #test_x=np.array(train_x).astype('float') #test_y=np.array(train_x).astype('float') import torch from torch.utils.data import TensorDataset, DataLoader # create Tensor datasets train_data = TensorDataset(torch.from_numpy(train_x), torch.from_numpy(train_y)) test_data = TensorDataset(torch.from_numpy(test_x), torch.from_numpy(test_y)) # dataloaders batch_size = 128 # make sure to SHUFFLE your data train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size) test_loader = DataLoader(test_data, shuffle=True, batch_size=batch_size) dataiter = iter(train_loader) sample_x, sample_y = dataiter.next() # print('Sample input size: ', sample_x.size()) # batch_size, seq_length # print('Sample input: \n', sample_x) # print() # print('Sample label size: ', sample_y.size()) # batch_size # print('Sample label: \n', sample_y) # First checking if GPU is available train_on_gpu=torch.cuda.is_available() if(train_on_gpu): print('Training on GPU.') else: print('No GPU available, training on CPU.') import torch.nn as nn class SentimentRNN(nn.Module): """ The RNN model that will be used to perform Sentiment analysis. """ def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, drop_prob=0.5): """ Initialize the model by setting up the layers. """ super(SentimentRNN, self).__init__() self.output_size = output_size self.n_layers = n_layers self.hidden_dim = hidden_dim # embedding and LSTM layers self.embedding = nn.Embedding(vocab_size, embedding_dim) self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout=drop_prob, batch_first=True) # dropout layer self.dropout = nn.Dropout(0.3) # linear and sigmoid layer self.fc = nn.Linear(hidden_dim, output_size) self.sig = nn.Sigmoid() def forward(self, x, hidden): """ Perform a forward pass of our model on some input and hidden state. """ batch_size = x.size(0) # embeddings and lstm_out embeds = self.embedding(x) lstm_out, hidden = self.lstm(embeds, hidden) # stack up lstm outputs lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim) # dropout and fully connected layer out = self.dropout(lstm_out) out = self.fc(out) # sigmoid function sig_out = self.sig(out) # reshape to be batch_size first sig_out = sig_out.view(batch_size, -1) sig_out = sig_out[:, -1] # get last batch of labels # return last sigmoid output and hidden state return sig_out, hidden def init_hidden(self, batch_size, train_on_gpu): ''' Initializes hidden state ''' # Create two new tensors with sizes n_layers x batch_size x hidden_dim, # initialized to zero, for hidden state and cell state of LSTM weight = next(self.parameters()).data if(train_on_gpu): hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda()) else: hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_(), weight.new(self.n_layers, batch_size, self.hidden_dim).zero_()) return hidden # Instantiate the model w/ hyperparams vocab_size = len(vocab_to_int) + 1 # +1 for zero padding + our word tokens output_size = 1 embedding_dim = 400 hidden_dim = 256 n_layers = 2 net = SentimentRNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers) print(net) # loss and optimization functions lr=0.001 criterion = nn.BCELoss() optimizer = torch.optim.Adam(net.parameters(), lr=lr) # training params epochs = 4 counter = 0 print_every = 100 clip=5 # gradient clipping train_on_gpu = True # move model to GPU, if available if(train_on_gpu): net.cuda() net.train() # train for some number of epochs for e in range(epochs): # initialize hidden state h = net.init_hidden(batch_size, train_on_gpu) counter = 0 # batch loop for inputs, labels in train_loader: counter += 1 #print('epoce: {e}, batch: {b}'.format(e=e, b=counter)) if (labels.shape[0] != batch_size): continue inputs = inputs.type(torch.LongTensor) labels = labels.type(torch.LongTensor) if(train_on_gpu): inputs, labels = inputs.cuda(), labels.cuda() # Creating new variables for the hidden state, otherwise # we'd backprop through the entire training history h = tuple([each.data for each in h]) # zero accumulated gradients net.zero_grad() output, h = net(inputs, h) # calculate the loss and perform backprop loss = criterion(output.squeeze(), labels.float()) loss.backward() # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs. nn.utils.clip_grad_norm_(net.parameters(), clip) optimizer.step() # Get test data loss and accuracy # = [] # track loss num_correct = 0 # init hidden state h = net.init_hidden(batch_size, train_on_gpu) counter=0 net.eval() all_prediction = [] # iterate over test data for inputs, labels in test
text=list(df_filtered['text']) stars=list(df_filtered['stars'])
random_line_split
Master Solution.py
processing and data analysis). Please comment your code appropriately. You will be evaluated for properly structuring your code and for building checks and balances in your analysis- which should be included in your code as well.* # # *2. If some data visualization tool such as Tableau/PowerBI is used for presentation of the plots in the panel round (if selected) then it will be considered a plus for the candidate. PPT presentation is acceptable though. The following visualizations are required- * # # **- Please prepare 1-2 slides to explain your data cleaning and processing steps, 1-2 slides to display the results of Task 1 (include the methodology used for completing the task), 1-2 slides to display the result of Task 2 (include the methodology used for completing the task), 1-2 slides on what other analysis is possible on the data set including the expected insights from those (for this you will need to mention the preferred methodology for text analysis). "** # - 9 You will be given a time limit of 3 Days from the time this test is given, to prepare the output. The candidates should upload the output docs- Dashboard/PPT & their 3 code files in a G-drive link and send them across to the assigned recruiter. # - 10 If your output gets selected, you will be asked to present your findings & approach to our panel of experts who would cross question you on your analysis. # # In[26]: import numpy as np import pandas as pd #for text processing import re import string import nltk from nltk.corpus import stopwords from textblob import Word #calculation of time from time import time ##pretty print import pprint # Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel from gensim.corpora import Dictionary # Build LDA model from gensim.models.ldamulticore import LdaMulticore import warnings warnings.filterwarnings("ignore",category=DeprecationWarning) # spacy import spacy # Plotting tools import pyLDAvis import pyLDAvis.gensim # don't skip this import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('max_colwidth', -1) # #### Merge all 3 sheets # In[2]: # from pandas import ExcelWriter # from pandas import ExcelFile xls = pd.ExcelFile('data.xlsx') df1 = pd.read_excel(xls, sheet_name='Aug') df2 = pd.read_excel(xls, sheet_name='Sept') df3 = pd.read_excel(xls, sheet_name='Oct') # In[3]: df = pd.concat([df1,df2,df3] , ignore_index=True) # ## Inspect Text field # In[4]: df.head() # In[5]: df.info() # In[6]: df.isnull().sum() # In[7]: #fetch missing values of a column df[df["Query Text"].isnull()] # In[8]: #drop all the rows which have NaN in Query Text df = df.dropna(how='any',axis=0) # In[9]: df.isnull().sum() # In[10]: df.drop_duplicates(subset ="Query Text", keep = 'last', inplace = True) # In[11]: df.info() # In[12]: # check the length of documents document_lengths = np.array(list(map(len, df['Query Text'].str.split(' ')))) print("The average number of words in a document is: {}.".format(np.mean(document_lengths))) print("The minimum number of words in a document is: {}.".format(min(document_lengths))) print("The maximum number of words in a document is: {}.".format(max(document_lengths))) # In[13]: print("There are {} documents with tops 5 words.".format(sum(document_lengths == 1))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 2))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 3))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 4))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 5))) # ## Task 1 # # ### Sub-task 2 : Text pre-processing # In[14]: def
(data): #convert text to lower-case data['processed_text'] = data['Query Text'].apply(lambda x:' '.join(x.lower() for x in x.split())) #remove punctuations, unwanted characters data['processed_text_1']= data['processed_text'].apply(lambda x: "".join([char for char in x if char not in string.punctuation])) #remove numbers data['processed_text_2']= data['processed_text_1'].apply(lambda x: re.sub('[0-9]+', ' ' , x)) #remove stopwords stop = stopwords.words('english') data['processed_text_3']= data['processed_text_2'].apply(lambda x:' '.join(x for x in x.split() if not x in stop)) #lemmatization data['processed_text_4']= data['processed_text_3'].apply(lambda x: " ".join([Word(word).lemmatize() for word in x.split()])) # remove all single characters data['processed_text_5'] = data['processed_text_4'].apply(lambda x: re.sub(r'\s+[a-zA-Z]\s+', ' ', x)) #create a final text field to work on data['final_text'] = data['processed_text_5'] # In[15]: #pre-processing or cleaning data text_preprocessing(df) df.head() # In[16]: #create tokenized data for LDA df['final_tokenized'] = list(map(nltk.word_tokenize, df.final_text)) df.head() # ## LDA training # In[17]: # Create Dictionary id2word = corpora.Dictionary(df['final_tokenized']) texts = df['final_tokenized'] # Term Document Frequency corpus = [id2word.doc2bow(text) for text in texts] # View print(corpus[:1]) # In[18]: id2word[0] # In[19]: # Human readable format of corpus (term-frequency) [[(id2word[id], freq) for id, freq in cp] for cp in corpus[:1]] # In[20]: get_ipython().run_cell_magic('time', '', "\nnum_topics = 10\n\nlda_model = LdaMulticore(corpus=corpus,\n id2word=id2word,\n num_topics=num_topics, \n workers=3, #CPU cores\n random_state=100,\n chunksize=400, #Number of documents to be used in each training chunk.\n passes=40, #Number of passes through the corpus during training.\n alpha='asymmetric',\n per_word_topics=True)") # In[27]: # View the topics in LDA model pp.pprint(lda_model.print_topics()) doc_lda = lda_model[corpus] # #### What is topic coeherence # # https://rare-technologies.com/what-is-topic-coherence/ # # What exactly is this topic coherence pipeline thing? Why is it even important? Moreover, what is the advantage of having this pipeline at all? In this post I will look to answer those questions in an as non-technical language as possible. This is meant for the general reader as much as a technical one so I will try to engage your imaginations more and your maths skills less. # # Imagine that you get water from a lot of places. The way you test this water is by providing it to a lot of people and then taking their reviews. If most of the reviews are bad, you say the water is bad and vice-versa. So basically all your evaluations are based on reviews with ratings as bad or good. If someone asks you exactly how good (or bad) the water is, you blend in your personal opinion. But this doesn’t assign a particular number to the quality of water and thus is only a qualitative analysis. Hence it can’t be used to compare two different sources of water in a definitive manner. # # Since you are a lazy person and strive to assign a quantity to the quality, you install four different pipes at the end of the water source and design a meter which tells you the exact quality of water by assigning a number to it. While doing this you receive help from a lot of wonderful people around you and therefore you are successful in installing it. Hence now you don’t need to go and gather hundred different people to get their opinion on the quality of water. You can get it straight from the meter and this value is always in accordance with the human opinions. # # The water here is the topics from some topic modelling algorithm. Earlier, the topics coming out from these topic modelling algorithms used to be tested on their human interpretability by presenting them to humans and taking their input on them. This was not quantitative but only qualitative. The meter and the pipes combined (yes you guessed it right) is the topic
text_preprocessing
identifier_name
Master Solution.py
data processing and data analysis). Please comment your code appropriately. You will be evaluated for properly structuring your code and for building checks and balances in your analysis- which should be included in your code as well.* # # *2. If some data visualization tool such as Tableau/PowerBI is used for presentation of the plots in the panel round (if selected) then it will be considered a plus for the candidate. PPT presentation is acceptable though. The following visualizations are required- * # # **- Please prepare 1-2 slides to explain your data cleaning and processing steps, 1-2 slides to display the results of Task 1 (include the methodology used for completing the task), 1-2 slides to display the result of Task 2 (include the methodology used for completing the task), 1-2 slides on what other analysis is possible on the data set including the expected insights from those (for this you will need to mention the preferred methodology for text analysis). "** # - 9 You will be given a time limit of 3 Days from the time this test is given, to prepare the output. The candidates should upload the output docs- Dashboard/PPT & their 3 code files in a G-drive link and send them across to the assigned recruiter. # - 10 If your output gets selected, you will be asked to present your findings & approach to our panel of experts who would cross question you on your analysis. # # In[26]: import numpy as np import pandas as pd #for text processing import re import string import nltk from nltk.corpus import stopwords from textblob import Word #calculation of time from time import time ##pretty print import pprint # Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel from gensim.corpora import Dictionary # Build LDA model from gensim.models.ldamulticore import LdaMulticore import warnings warnings.filterwarnings("ignore",category=DeprecationWarning) # spacy import spacy # Plotting tools import pyLDAvis import pyLDAvis.gensim # don't skip this import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('max_colwidth', -1) # #### Merge all 3 sheets # In[2]: # from pandas import ExcelWriter # from pandas import ExcelFile xls = pd.ExcelFile('data.xlsx') df1 = pd.read_excel(xls, sheet_name='Aug') df2 = pd.read_excel(xls, sheet_name='Sept') df3 = pd.read_excel(xls, sheet_name='Oct') # In[3]: df = pd.concat([df1,df2,df3] , ignore_index=True) # ## Inspect Text field # In[4]: df.head() # In[5]: df.info() # In[6]: df.isnull().sum() # In[7]: #fetch missing values of a column df[df["Query Text"].isnull()] # In[8]: #drop all the rows which have NaN in Query Text df = df.dropna(how='any',axis=0)
# In[9]: df.isnull().sum() # In[10]: df.drop_duplicates(subset ="Query Text", keep = 'last', inplace = True) # In[11]: df.info() # In[12]: # check the length of documents document_lengths = np.array(list(map(len, df['Query Text'].str.split(' ')))) print("The average number of words in a document is: {}.".format(np.mean(document_lengths))) print("The minimum number of words in a document is: {}.".format(min(document_lengths))) print("The maximum number of words in a document is: {}.".format(max(document_lengths))) # In[13]: print("There are {} documents with tops 5 words.".format(sum(document_lengths == 1))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 2))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 3))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 4))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 5))) # ## Task 1 # # ### Sub-task 2 : Text pre-processing # In[14]: def text_preprocessing(data): #convert text to lower-case data['processed_text'] = data['Query Text'].apply(lambda x:' '.join(x.lower() for x in x.split())) #remove punctuations, unwanted characters data['processed_text_1']= data['processed_text'].apply(lambda x: "".join([char for char in x if char not in string.punctuation])) #remove numbers data['processed_text_2']= data['processed_text_1'].apply(lambda x: re.sub('[0-9]+', ' ' , x)) #remove stopwords stop = stopwords.words('english') data['processed_text_3']= data['processed_text_2'].apply(lambda x:' '.join(x for x in x.split() if not x in stop)) #lemmatization data['processed_text_4']= data['processed_text_3'].apply(lambda x: " ".join([Word(word).lemmatize() for word in x.split()])) # remove all single characters data['processed_text_5'] = data['processed_text_4'].apply(lambda x: re.sub(r'\s+[a-zA-Z]\s+', ' ', x)) #create a final text field to work on data['final_text'] = data['processed_text_5'] # In[15]: #pre-processing or cleaning data text_preprocessing(df) df.head() # In[16]: #create tokenized data for LDA df['final_tokenized'] = list(map(nltk.word_tokenize, df.final_text)) df.head() # ## LDA training # In[17]: # Create Dictionary id2word = corpora.Dictionary(df['final_tokenized']) texts = df['final_tokenized'] # Term Document Frequency corpus = [id2word.doc2bow(text) for text in texts] # View print(corpus[:1]) # In[18]: id2word[0] # In[19]: # Human readable format of corpus (term-frequency) [[(id2word[id], freq) for id, freq in cp] for cp in corpus[:1]] # In[20]: get_ipython().run_cell_magic('time', '', "\nnum_topics = 10\n\nlda_model = LdaMulticore(corpus=corpus,\n id2word=id2word,\n num_topics=num_topics, \n workers=3, #CPU cores\n random_state=100,\n chunksize=400, #Number of documents to be used in each training chunk.\n passes=40, #Number of passes through the corpus during training.\n alpha='asymmetric',\n per_word_topics=True)") # In[27]: # View the topics in LDA model pp.pprint(lda_model.print_topics()) doc_lda = lda_model[corpus] # #### What is topic coeherence # # https://rare-technologies.com/what-is-topic-coherence/ # # What exactly is this topic coherence pipeline thing? Why is it even important? Moreover, what is the advantage of having this pipeline at all? In this post I will look to answer those questions in an as non-technical language as possible. This is meant for the general reader as much as a technical one so I will try to engage your imaginations more and your maths skills less. # # Imagine that you get water from a lot of places. The way you test this water is by providing it to a lot of people and then taking their reviews. If most of the reviews are bad, you say the water is bad and vice-versa. So basically all your evaluations are based on reviews with ratings as bad or good. If someone asks you exactly how good (or bad) the water is, you blend in your personal opinion. But this doesn’t assign a particular number to the quality of water and thus is only a qualitative analysis. Hence it can’t be used to compare two different sources of water in a definitive manner. # # Since you are a lazy person and strive to assign a quantity to the quality, you install four different pipes at the end of the water source and design a meter which tells you the exact quality of water by assigning a number to it. While doing this you receive help from a lot of wonderful people around you and therefore you are successful in installing it. Hence now you don’t need to go and gather hundred different people to get their opinion on the quality of water. You can get it straight from the meter and this value is always in accordance with the human opinions. # # The water here is the topics from some topic modelling algorithm. Earlier, the topics coming out from these topic modelling algorithms used to be tested on their human interpretability by presenting them to humans and taking their input on them. This was not quantitative but only qualitative. The meter and the pipes combined (yes you guessed it right) is the
random_line_split
Master Solution.py
processing and data analysis). Please comment your code appropriately. You will be evaluated for properly structuring your code and for building checks and balances in your analysis- which should be included in your code as well.* # # *2. If some data visualization tool such as Tableau/PowerBI is used for presentation of the plots in the panel round (if selected) then it will be considered a plus for the candidate. PPT presentation is acceptable though. The following visualizations are required- * # # **- Please prepare 1-2 slides to explain your data cleaning and processing steps, 1-2 slides to display the results of Task 1 (include the methodology used for completing the task), 1-2 slides to display the result of Task 2 (include the methodology used for completing the task), 1-2 slides on what other analysis is possible on the data set including the expected insights from those (for this you will need to mention the preferred methodology for text analysis). "** # - 9 You will be given a time limit of 3 Days from the time this test is given, to prepare the output. The candidates should upload the output docs- Dashboard/PPT & their 3 code files in a G-drive link and send them across to the assigned recruiter. # - 10 If your output gets selected, you will be asked to present your findings & approach to our panel of experts who would cross question you on your analysis. # # In[26]: import numpy as np import pandas as pd #for text processing import re import string import nltk from nltk.corpus import stopwords from textblob import Word #calculation of time from time import time ##pretty print import pprint # Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel from gensim.corpora import Dictionary # Build LDA model from gensim.models.ldamulticore import LdaMulticore import warnings warnings.filterwarnings("ignore",category=DeprecationWarning) # spacy import spacy # Plotting tools import pyLDAvis import pyLDAvis.gensim # don't skip this import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('max_colwidth', -1) # #### Merge all 3 sheets # In[2]: # from pandas import ExcelWriter # from pandas import ExcelFile xls = pd.ExcelFile('data.xlsx') df1 = pd.read_excel(xls, sheet_name='Aug') df2 = pd.read_excel(xls, sheet_name='Sept') df3 = pd.read_excel(xls, sheet_name='Oct') # In[3]: df = pd.concat([df1,df2,df3] , ignore_index=True) # ## Inspect Text field # In[4]: df.head() # In[5]: df.info() # In[6]: df.isnull().sum() # In[7]: #fetch missing values of a column df[df["Query Text"].isnull()] # In[8]: #drop all the rows which have NaN in Query Text df = df.dropna(how='any',axis=0) # In[9]: df.isnull().sum() # In[10]: df.drop_duplicates(subset ="Query Text", keep = 'last', inplace = True) # In[11]: df.info() # In[12]: # check the length of documents document_lengths = np.array(list(map(len, df['Query Text'].str.split(' ')))) print("The average number of words in a document is: {}.".format(np.mean(document_lengths))) print("The minimum number of words in a document is: {}.".format(min(document_lengths))) print("The maximum number of words in a document is: {}.".format(max(document_lengths))) # In[13]: print("There are {} documents with tops 5 words.".format(sum(document_lengths == 1))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 2))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 3))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 4))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 5))) # ## Task 1 # # ### Sub-task 2 : Text pre-processing # In[14]: def text_preprocessing(data): #convert text to lower-case
# In[15]: #pre-processing or cleaning data text_preprocessing(df) df.head() # In[16]: #create tokenized data for LDA df['final_tokenized'] = list(map(nltk.word_tokenize, df.final_text)) df.head() # ## LDA training # In[17]: # Create Dictionary id2word = corpora.Dictionary(df['final_tokenized']) texts = df['final_tokenized'] # Term Document Frequency corpus = [id2word.doc2bow(text) for text in texts] # View print(corpus[:1]) # In[18]: id2word[0] # In[19]: # Human readable format of corpus (term-frequency) [[(id2word[id], freq) for id, freq in cp] for cp in corpus[:1]] # In[20]: get_ipython().run_cell_magic('time', '', "\nnum_topics = 10\n\nlda_model = LdaMulticore(corpus=corpus,\n id2word=id2word,\n num_topics=num_topics, \n workers=3, #CPU cores\n random_state=100,\n chunksize=400, #Number of documents to be used in each training chunk.\n passes=40, #Number of passes through the corpus during training.\n alpha='asymmetric',\n per_word_topics=True)") # In[27]: # View the topics in LDA model pp.pprint(lda_model.print_topics()) doc_lda = lda_model[corpus] # #### What is topic coeherence # # https://rare-technologies.com/what-is-topic-coherence/ # # What exactly is this topic coherence pipeline thing? Why is it even important? Moreover, what is the advantage of having this pipeline at all? In this post I will look to answer those questions in an as non-technical language as possible. This is meant for the general reader as much as a technical one so I will try to engage your imaginations more and your maths skills less. # # Imagine that you get water from a lot of places. The way you test this water is by providing it to a lot of people and then taking their reviews. If most of the reviews are bad, you say the water is bad and vice-versa. So basically all your evaluations are based on reviews with ratings as bad or good. If someone asks you exactly how good (or bad) the water is, you blend in your personal opinion. But this doesn’t assign a particular number to the quality of water and thus is only a qualitative analysis. Hence it can’t be used to compare two different sources of water in a definitive manner. # # Since you are a lazy person and strive to assign a quantity to the quality, you install four different pipes at the end of the water source and design a meter which tells you the exact quality of water by assigning a number to it. While doing this you receive help from a lot of wonderful people around you and therefore you are successful in installing it. Hence now you don’t need to go and gather hundred different people to get their opinion on the quality of water. You can get it straight from the meter and this value is always in accordance with the human opinions. # # The water here is the topics from some topic modelling algorithm. Earlier, the topics coming out from these topic modelling algorithms used to be tested on their human interpretability by presenting them to humans and taking their input on them. This was not quantitative but only qualitative. The meter and the pipes combined (yes you guessed it right) is the topic
data['processed_text'] = data['Query Text'].apply(lambda x:' '.join(x.lower() for x in x.split())) #remove punctuations, unwanted characters data['processed_text_1']= data['processed_text'].apply(lambda x: "".join([char for char in x if char not in string.punctuation])) #remove numbers data['processed_text_2']= data['processed_text_1'].apply(lambda x: re.sub('[0-9]+', ' ' , x)) #remove stopwords stop = stopwords.words('english') data['processed_text_3']= data['processed_text_2'].apply(lambda x:' '.join(x for x in x.split() if not x in stop)) #lemmatization data['processed_text_4']= data['processed_text_3'].apply(lambda x: " ".join([Word(word).lemmatize() for word in x.split()])) # remove all single characters data['processed_text_5'] = data['processed_text_4'].apply(lambda x: re.sub(r'\s+[a-zA-Z]\s+', ' ', x)) #create a final text field to work on data['final_text'] = data['processed_text_5']
identifier_body
Master Solution.py
: {}.".format(min(document_lengths))) print("The maximum number of words in a document is: {}.".format(max(document_lengths))) # In[13]: print("There are {} documents with tops 5 words.".format(sum(document_lengths == 1))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 2))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 3))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 4))) print("There are {} documents with tops 5 words.".format(sum(document_lengths == 5))) # ## Task 1 # # ### Sub-task 2 : Text pre-processing # In[14]: def text_preprocessing(data): #convert text to lower-case data['processed_text'] = data['Query Text'].apply(lambda x:' '.join(x.lower() for x in x.split())) #remove punctuations, unwanted characters data['processed_text_1']= data['processed_text'].apply(lambda x: "".join([char for char in x if char not in string.punctuation])) #remove numbers data['processed_text_2']= data['processed_text_1'].apply(lambda x: re.sub('[0-9]+', ' ' , x)) #remove stopwords stop = stopwords.words('english') data['processed_text_3']= data['processed_text_2'].apply(lambda x:' '.join(x for x in x.split() if not x in stop)) #lemmatization data['processed_text_4']= data['processed_text_3'].apply(lambda x: " ".join([Word(word).lemmatize() for word in x.split()])) # remove all single characters data['processed_text_5'] = data['processed_text_4'].apply(lambda x: re.sub(r'\s+[a-zA-Z]\s+', ' ', x)) #create a final text field to work on data['final_text'] = data['processed_text_5'] # In[15]: #pre-processing or cleaning data text_preprocessing(df) df.head() # In[16]: #create tokenized data for LDA df['final_tokenized'] = list(map(nltk.word_tokenize, df.final_text)) df.head() # ## LDA training # In[17]: # Create Dictionary id2word = corpora.Dictionary(df['final_tokenized']) texts = df['final_tokenized'] # Term Document Frequency corpus = [id2word.doc2bow(text) for text in texts] # View print(corpus[:1]) # In[18]: id2word[0] # In[19]: # Human readable format of corpus (term-frequency) [[(id2word[id], freq) for id, freq in cp] for cp in corpus[:1]] # In[20]: get_ipython().run_cell_magic('time', '', "\nnum_topics = 10\n\nlda_model = LdaMulticore(corpus=corpus,\n id2word=id2word,\n num_topics=num_topics, \n workers=3, #CPU cores\n random_state=100,\n chunksize=400, #Number of documents to be used in each training chunk.\n passes=40, #Number of passes through the corpus during training.\n alpha='asymmetric',\n per_word_topics=True)") # In[27]: # View the topics in LDA model pp.pprint(lda_model.print_topics()) doc_lda = lda_model[corpus] # #### What is topic coeherence # # https://rare-technologies.com/what-is-topic-coherence/ # # What exactly is this topic coherence pipeline thing? Why is it even important? Moreover, what is the advantage of having this pipeline at all? In this post I will look to answer those questions in an as non-technical language as possible. This is meant for the general reader as much as a technical one so I will try to engage your imaginations more and your maths skills less. # # Imagine that you get water from a lot of places. The way you test this water is by providing it to a lot of people and then taking their reviews. If most of the reviews are bad, you say the water is bad and vice-versa. So basically all your evaluations are based on reviews with ratings as bad or good. If someone asks you exactly how good (or bad) the water is, you blend in your personal opinion. But this doesn’t assign a particular number to the quality of water and thus is only a qualitative analysis. Hence it can’t be used to compare two different sources of water in a definitive manner. # # Since you are a lazy person and strive to assign a quantity to the quality, you install four different pipes at the end of the water source and design a meter which tells you the exact quality of water by assigning a number to it. While doing this you receive help from a lot of wonderful people around you and therefore you are successful in installing it. Hence now you don’t need to go and gather hundred different people to get their opinion on the quality of water. You can get it straight from the meter and this value is always in accordance with the human opinions. # # The water here is the topics from some topic modelling algorithm. Earlier, the topics coming out from these topic modelling algorithms used to be tested on their human interpretability by presenting them to humans and taking their input on them. This was not quantitative but only qualitative. The meter and the pipes combined (yes you guessed it right) is the topic coherence pipeline. The four pipes are: # # Segmentation : Where the water is partitioned into several glasses assuming that the quality of water in each glass is different. # Probability Estimation : Where the quantity of water in each glass is measured. # Confirmation Measure : Where the quality of water (according to a certain metric) in each glass is measured and a number is assigned to each glass wrt it’s quantity. # Aggregation : The meter where these quality numbers are combined in a certain way (say arithmetic mean) to come up with one number. # And there you have your topic coherence pipeline! There are surely much better analogies than this one but I hope you got the gist of it. # In[28]: get_ipython().run_cell_magic('time', '', "\n# Compute Perplexity\nprint('\\nPerplexity: ', lda_model.log_perplexity(corpus)) # a measure of how good the model is. lower the better.\n\n# Compute Coherence Score\ncoherence_model_lda = CoherenceModel(model=lda_model, texts=df['final_tokenized'], dictionary=id2word, coherence='c_v')\ncoherence_lda = coherence_model_lda.get_coherence()\nprint('\\nCoherence Score: ', coherence_lda)") # ## Top 10 topics by frequency of occurence # # # In[29]: get_ipython().run_cell_magic('time', '', '\n# Visualize the topics\n\npyLDAvis.enable_notebook()\nvis = pyLDAvis.gensim.prepare(lda_model, corpus, id2word)\nvis') # #### How to find the optimal number of topics for LDA? # # My approach to finding the optimal number of topics is to build many LDA models with different values of number of topics (k) and pick the one that gives the highest coherence value. # # Choosing a ‘k’ that marks the end of a rapid growth of topic coherence usually offers meaningful and interpretable topics. Picking an even higher value can sometimes provide more granular sub-topics. # # If you see the same keywords being repeated in multiple topics, it’s probably a sign that the ‘k’ is too large. # # The compute_coherence_values() (see below) trains multiple LDA models and provides the models and their corresponding coherence scores. # If the coherence score seems to keep increasing, it may make better sense to pick the model that gave the highest CV before flattening out. This is exactly the case here. # # So for further steps I will choose the model with 20 topics itself. # ## Sub-Task2 Named Entity Recognition # In[34]: from IPython.display import Image Image("img/picture.png") # In[35]: import spacy from spacy import displacy from collections import Counter import en_core_web_sm nlp = en_core_web_sm.load() # In[36]: #removing duplicates final_text = df['final_text'].unique() print('Number of Query Text: ', len(final_text)) # In[37]: corpus = list(nlp.pipe(final_text)) # In[38]: # Looking at number of times each ent appears in the total corpus # nb. ents all appear as Spacy tokens, hence needing to cast as str from collections import defaultdict all_ents = defaultdict(int) for i, doc in enumerate(corpus): #print(i,doc) for ent in doc.ents: all_ents[str(ent)] += 1 #print(ent) print('Number of distinct entities: ', len(all_ents)) # In[39]: # labels = [x.label_ for x in corpus.ents] # Counter(labels) ent_label = [] ent_common = [] for i, doc in enumerate(corpus): for ent in doc.ents: ent_label.append(e
nt.label_) ent_common.append(ent.text) print("U
conditional_block
helpers.go
updated string // with replaced coordinates and the list of coordinates func ExtractCoordinates(text string) (string, Coordinates) { var ( // <a href="geo:49.976136, 36.267256">49.976136, 36.267256</a> geoHrefRe = regexp.MustCompile("<a.+?href=\"geo:(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})\">(.+?)</a>") // <a href="https://www.google.com.ua/maps/@50.0363257,36.2120039,19z" target="blank">50.036435 36.211914</a> hrefRe = regexp.MustCompile("<a.+?href=\"https?://.+?(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,}).*?\">(.+?)</a>") // 49.976136, 36.267256 numbersRe = regexp.MustCompile("(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})") res = text coords = Coordinates{} tmpCoords Coordinates ) log.Print("[INFO] Extract coordinates from task text") for _, re := range []*regexp.Regexp{geoHrefRe, hrefRe, numbersRe} { res, tmpCoords = extractCoordinates(res, re) coords = append(coords, tmpCoords...) } for _, coord := range coords { res = strings.Replace(res, "#coords#", coord.OriginalString, 1) } if DEBUG { log.Printf("[DEBUG] Found %d coordinates", len(coords)) } return res, coords } func extractImages(text string, re *regexp.Regexp, caption string, start int) (string, Images) { var ( result = text mr = re.FindAllStringSubmatch(text, -1) images = Images{} ) if len(mr) > 0 { for i, item := range mr { images = append(images, Image{URL: item[1], Caption: fmt.Sprintf("%s #%d", caption, start+i)}) result = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(result, fmt.Sprintf("%s #%d", caption, start+i)) } } return result, images } // ExtractImages extracts images from the given text and returns the updated // version of the text and the list of images func ExtractImages(text string, caption string) (string, Images) { var ( reImg = regexp.MustCompile("<img.+?src=\"\\s*(https?://.+?)\\s*\".*?>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(https?://.+?\\.(jpg|png|bmp))\\\\?\".*?>(.*?)</a>") result = text images = Images{} tmpImages Images ) //log.Printf("Before image replacing: %s", text) log.Print("[INFO] Extract images from task text") for _, re := range []*regexp.Regexp{reImg, reA} { result, tmpImages = extractImages(result, re, caption, len(images)+1) images = append(images, tmpImages...) } if DEBUG { log.Printf("[DEBUG] Found %d images", len(images)) } return result, images } // ReplaceHTMLTags finds all html tags and removes them. Some tags like bold, italic are replaed with // makrkups for telegram func ReplaceHTMLTags(text string) string { var ( parser = html.NewTokenizer(strings.NewReader(text)) tagStack = stack.New() textToTag = map[int]string{} ) for { node := parser.Next() switch node { case html.ErrorToken: result := strings.Replace(textToTag[0], "&nbsp;", " ", -1) return result case html.TextToken: t := string(parser.Text()) textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], t}, "") case html.StartTagToken: tagName, hasAttr := parser.TagName() if string(tagName) == scriptTag { // We can skip script tags, as they are invisible for the user, but we can indicate that there are // scripts in the task. To skip tag, it is necessary to call Next() two times: // 1) returns TextToken with the script body // 2) returns EndTagToken for the closed script tag // Usually script tag doesn't have any neste tags, so this aproach should work log.Printf("[INFO] Skipping script tag") parser.Next() parser.Next() continue } tag := Tag{Tag: string(tagName), Attrs: map[string]string{}} if hasAttr { for { attr, val, moreAttr := parser.TagAttr() if DEBUG { log.Printf("[DEBUG] Found attr %s", attr) } tag.Attrs[string(attr)] = string(val) if !moreAttr { break } } } if DEBUG { log.Printf("[DEBUG] Found tag %q", tag) } tagStack.Push(tag) case html.EndTagToken: var ( addText string tagNo = tagStack.Len() tag = tagStack.Pop() closedTag, _ = parser.TagName() ) if tag.(Tag).Tag != string(closedTag) { log.Printf("[WARNING] Found closed tag %q but expected %q", closedTag, tag) continue } if DEBUG { log.Printf("[DEBUG] Found end of tag %q", closedTag) } switch tag.(Tag).Tag { case iTag: addText = fmt.Sprintf("_%s_", textToTag[tagNo]) case bTag, strongTag: addText = fmt.Sprintf("*%s*", textToTag[tagNo]) case aTag: // if strings.Compare(string(attr), "href") == 0 { addText = fmt.Sprintf("[%s](%s)", textToTag[tagNo], tag.(Tag).Attrs["href"]) // } default: addText = textToTag[tagNo] } textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], addText}, "") delete(textToTag, tagNo) } } } // ReplaceCommonTags deprecated - should be removed!!! func ReplaceCommonTags(text string) string { log.Print("Replace html tags") var ( reBr = regexp.MustCompile("<br\\s*/?>") reHr = regexp.MustCompile("<hr.*?/?>") reP = regexp.MustCompile("<p>([^ ]+?)</p>") reBold = regexp.MustCompile("<b.*?/?>((?s:.*?))</b>") reStrong = regexp.MustCompile("<strong.*?>(.*?)</strong>") reItalic = regexp.MustCompile("<i>((?s:.+?))</i>") reSpan = regexp.MustCompile("<span.*?>(.*?)</span>") reCenter = regexp.MustCompile("<center>((?s:.*?))</center>") reFont = regexp.MustCompile("<font.+?color\\s*=\\\\?[\"«]?#?(\\w+)\\\\?[\"»]?.*?>((?s:.*?))</font>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(.+?)\\\\?\".*?>(.+?)</a>") res = text ) res = strings.Replace(text, "_", "\\_", -1) if mrBr := reBr.FindAllStringSubmatch(text, -1); len(mrBr) > 0 {
if mrHr := reHr.FindAllStringSubmatch(res, -1); len(mrHr) > 0 { for _, item := range mrHr { res = regexp.MustCompile(item[0]).ReplaceAllLiteralString(res, "\n") } } if mrP := reP.FindAllStringSubmatch(res, -1); len(mrP) > 0 { for _, item := range mrP { res = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(res, fmt.Sprintf("\n%s", item[1])) } } if mrFont := reFont.FindAllStringSubmatch(res, -1); len(mrFont) > 0 { for _, item := range mrFont { res = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(res, fmt.Sprintf("%s", item[2])) //ReplaceAllLiteral(res, []byte(fmt.Sprintf("#%s#%s#", item[1], item[2]))) } } if mr
for _, item := range mrBr { res = regexp.MustCompile(item[0]).ReplaceAllLiteralString(res, "\n") } }
conditional_block
helpers.go
updated string // with replaced coordinates and the list of coordinates func ExtractCoordinates(text string) (string, Coordinates) { var ( // <a href="geo:49.976136, 36.267256">49.976136, 36.267256</a> geoHrefRe = regexp.MustCompile("<a.+?href=\"geo:(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})\">(.+?)</a>") // <a href="https://www.google.com.ua/maps/@50.0363257,36.2120039,19z" target="blank">50.036435 36.211914</a> hrefRe = regexp.MustCompile("<a.+?href=\"https?://.+?(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,}).*?\">(.+?)</a>") // 49.976136, 36.267256 numbersRe = regexp.MustCompile("(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})") res = text coords = Coordinates{} tmpCoords Coordinates ) log.Print("[INFO] Extract coordinates from task text") for _, re := range []*regexp.Regexp{geoHrefRe, hrefRe, numbersRe} { res, tmpCoords = extractCoordinates(res, re) coords = append(coords, tmpCoords...) } for _, coord := range coords { res = strings.Replace(res, "#coords#", coord.OriginalString, 1) } if DEBUG { log.Printf("[DEBUG] Found %d coordinates", len(coords)) } return res, coords } func extractImages(text string, re *regexp.Regexp, caption string, start int) (string, Images) { var ( result = text mr = re.FindAllStringSubmatch(text, -1) images = Images{} ) if len(mr) > 0 { for i, item := range mr { images = append(images, Image{URL: item[1], Caption: fmt.Sprintf("%s #%d", caption, start+i)}) result = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(result, fmt.Sprintf("%s #%d", caption, start+i)) } } return result, images } // ExtractImages extracts images from the given text and returns the updated // version of the text and the list of images func ExtractImages(text string, caption string) (string, Images) { var ( reImg = regexp.MustCompile("<img.+?src=\"\\s*(https?://.+?)\\s*\".*?>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(https?://.+?\\.(jpg|png|bmp))\\\\?\".*?>(.*?)</a>") result = text images = Images{} tmpImages Images ) //log.Printf("Before image replacing: %s", text) log.Print("[INFO] Extract images from task text") for _, re := range []*regexp.Regexp{reImg, reA} { result, tmpImages = extractImages(result, re, caption, len(images)+1) images = append(images, tmpImages...) } if DEBUG { log.Printf("[DEBUG] Found %d images", len(images)) } return result, images } // ReplaceHTMLTags finds all html tags and removes them. Some tags like bold, italic are replaed with // makrkups for telegram func ReplaceHTMLTags(text string) string
// scripts in the task. To skip tag, it is necessary to call Next() two times: // 1) returns TextToken with the script body // 2) returns EndTagToken for the closed script tag // Usually script tag doesn't have any neste tags, so this aproach should work log.Printf("[INFO] Skipping script tag") parser.Next() parser.Next() continue } tag := Tag{Tag: string(tagName), Attrs: map[string]string{}} if hasAttr { for { attr, val, moreAttr := parser.TagAttr() if DEBUG { log.Printf("[DEBUG] Found attr %s", attr) } tag.Attrs[string(attr)] = string(val) if !moreAttr { break } } } if DEBUG { log.Printf("[DEBUG] Found tag %q", tag) } tagStack.Push(tag) case html.EndTagToken: var ( addText string tagNo = tagStack.Len() tag = tagStack.Pop() closedTag, _ = parser.TagName() ) if tag.(Tag).Tag != string(closedTag) { log.Printf("[WARNING] Found closed tag %q but expected %q", closedTag, tag) continue } if DEBUG { log.Printf("[DEBUG] Found end of tag %q", closedTag) } switch tag.(Tag).Tag { case iTag: addText = fmt.Sprintf("_%s_", textToTag[tagNo]) case bTag, strongTag: addText = fmt.Sprintf("*%s*", textToTag[tagNo]) case aTag: // if strings.Compare(string(attr), "href") == 0 { addText = fmt.Sprintf("[%s](%s)", textToTag[tagNo], tag.(Tag).Attrs["href"]) // } default: addText = textToTag[tagNo] } textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], addText}, "") delete(textToTag, tagNo) } } } // ReplaceCommonTags deprecated - should be removed!!! func ReplaceCommonTags(text string) string { log.Print("Replace html tags") var ( reBr = regexp.MustCompile("<br\\s*/?>") reHr = regexp.MustCompile("<hr.*?/?>") reP = regexp.MustCompile("<p>([^ ]+?)</p>") reBold = regexp.MustCompile("<b.*?/?>((?s:.*?))</b>") reStrong = regexp.MustCompile("<strong.*?>(.*?)</strong>") reItalic = regexp.MustCompile("<i>((?s:.+?))</i>") reSpan = regexp.MustCompile("<span.*?>(.*?)</span>") reCenter = regexp.MustCompile("<center>((?s:.*?))</center>") reFont = regexp.MustCompile("<font.+?color\\s*=\\\\?[\"«]?#?(\\w+)\\\\?[\"»]?.*?>((?s:.*?))</font>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(.+?)\\\\?\".*?>(.+?)</a>") res = text ) res = strings.Replace(text, "_", "\\_", -1) if mrBr := reBr.FindAllStringSubmatch(text, -1); len(mrBr) > 0 { for _, item := range mrBr { res = regexp.MustCompile(item[0]).ReplaceAllLiteralString(res, "\n") } } if mrHr := reHr.FindAllStringSubmatch(res, -1); len(mrHr) > 0 { for _, item := range mrHr { res = regexp.MustCompile(item[0]).ReplaceAllLiteralString(res, "\n") } } if mrP := reP.FindAllStringSubmatch(res, -1); len(mrP) > 0 { for _, item := range mrP { res = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(res, fmt.Sprintf("\n%s", item[1])) } } if mrFont := reFont.FindAllStringSubmatch(res, -1); len(mrFont) > 0 { for _, item := range mrFont { res = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(res, fmt.Sprintf("%s", item[2])) //ReplaceAllLiteral(res, []byte(fmt.Sprintf("#%s#%s#", item[1], item[2]))) } } if mrBold
{ var ( parser = html.NewTokenizer(strings.NewReader(text)) tagStack = stack.New() textToTag = map[int]string{} ) for { node := parser.Next() switch node { case html.ErrorToken: result := strings.Replace(textToTag[0], "&nbsp;", " ", -1) return result case html.TextToken: t := string(parser.Text()) textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], t}, "") case html.StartTagToken: tagName, hasAttr := parser.TagName() if string(tagName) == scriptTag { // We can skip script tags, as they are invisible for the user, but we can indicate that there are
identifier_body
helpers.go
updated string // with replaced coordinates and the list of coordinates func ExtractCoordinates(text string) (string, Coordinates) { var ( // <a href="geo:49.976136, 36.267256">49.976136, 36.267256</a> geoHrefRe = regexp.MustCompile("<a.+?href=\"geo:(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})\">(.+?)</a>") // <a href="https://www.google.com.ua/maps/@50.0363257,36.2120039,19z" target="blank">50.036435 36.211914</a> hrefRe = regexp.MustCompile("<a.+?href=\"https?://.+?(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,}).*?\">(.+?)</a>") // 49.976136, 36.267256 numbersRe = regexp.MustCompile("(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})") res = text coords = Coordinates{} tmpCoords Coordinates ) log.Print("[INFO] Extract coordinates from task text") for _, re := range []*regexp.Regexp{geoHrefRe, hrefRe, numbersRe} { res, tmpCoords = extractCoordinates(res, re) coords = append(coords, tmpCoords...) } for _, coord := range coords { res = strings.Replace(res, "#coords#", coord.OriginalString, 1) } if DEBUG { log.Printf("[DEBUG] Found %d coordinates", len(coords)) } return res, coords } func extractImages(text string, re *regexp.Regexp, caption string, start int) (string, Images) { var ( result = text mr = re.FindAllStringSubmatch(text, -1) images = Images{} ) if len(mr) > 0 { for i, item := range mr { images = append(images, Image{URL: item[1], Caption: fmt.Sprintf("%s #%d", caption, start+i)}) result = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(result, fmt.Sprintf("%s #%d", caption, start+i)) } } return result, images } // ExtractImages extracts images from the given text and returns the updated // version of the text and the list of images func ExtractImages(text string, caption string) (string, Images) { var ( reImg = regexp.MustCompile("<img.+?src=\"\\s*(https?://.+?)\\s*\".*?>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(https?://.+?\\.(jpg|png|bmp))\\\\?\".*?>(.*?)</a>") result = text images = Images{} tmpImages Images ) //log.Printf("Before image replacing: %s", text) log.Print("[INFO] Extract images from task text") for _, re := range []*regexp.Regexp{reImg, reA} { result, tmpImages = extractImages(result, re, caption, len(images)+1) images = append(images, tmpImages...) } if DEBUG { log.Printf("[DEBUG] Found %d images", len(images)) } return result, images } // ReplaceHTMLTags finds all html tags and removes them. Some tags like bold, italic are replaed with // makrkups for telegram func ReplaceHTMLTags(text string) string { var ( parser = html.NewTokenizer(strings.NewReader(text)) tagStack = stack.New() textToTag = map[int]string{} ) for { node := parser.Next() switch node { case html.ErrorToken: result := strings.Replace(textToTag[0], "&nbsp;", " ", -1) return result case html.TextToken: t := string(parser.Text()) textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], t}, "") case html.StartTagToken: tagName, hasAttr := parser.TagName() if string(tagName) == scriptTag { // We can skip script tags, as they are invisible for the user, but we can indicate that there are // scripts in the task. To skip tag, it is necessary to call Next() two times: // 1) returns TextToken with the script body // 2) returns EndTagToken for the closed script tag // Usually script tag doesn't have any neste tags, so this aproach should work log.Printf("[INFO] Skipping script tag") parser.Next() parser.Next() continue } tag := Tag{Tag: string(tagName), Attrs: map[string]string{}} if hasAttr { for { attr, val, moreAttr := parser.TagAttr() if DEBUG { log.Printf("[DEBUG] Found attr %s", attr) } tag.Attrs[string(attr)] = string(val) if !moreAttr { break } } } if DEBUG { log.Printf("[DEBUG] Found tag %q", tag) } tagStack.Push(tag) case html.EndTagToken: var ( addText string tagNo = tagStack.Len() tag = tagStack.Pop() closedTag, _ = parser.TagName() ) if tag.(Tag).Tag != string(closedTag) { log.Printf("[WARNING] Found closed tag %q but expected %q", closedTag, tag) continue } if DEBUG { log.Printf("[DEBUG] Found end of tag %q", closedTag) } switch tag.(Tag).Tag { case iTag: addText = fmt.Sprintf("_%s_", textToTag[tagNo]) case bTag, strongTag: addText = fmt.Sprintf("*%s*", textToTag[tagNo]) case aTag: // if strings.Compare(string(attr), "href") == 0 { addText = fmt.Sprintf("[%s](%s)", textToTag[tagNo], tag.(Tag).Attrs["href"]) // } default: addText = textToTag[tagNo] } textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], addText}, "") delete(textToTag, tagNo) } } } // ReplaceCommonTags deprecated - should be removed!!! func ReplaceCommonTags(text string) string { log.Print("Replace html tags") var ( reBr = regexp.MustCompile("<br\\s*/?>") reHr = regexp.MustCompile("<hr.*?/?>") reP = regexp.MustCompile("<p>([^ ]+?)</p>") reBold = regexp.MustCompile("<b.*?/?>((?s:.*?))</b>")
reCenter = regexp.MustCompile("<center>((?s:.*?))</center>") reFont = regexp.MustCompile("<font.+?color\\s*=\\\\?[\"«]?#?(\\w+)\\\\?[\"»]?.*?>((?s:.*?))</font>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(.+?)\\\\?\".*?>(.+?)</a>") res = text ) res = strings.Replace(text, "_", "\\_", -1) if mrBr := reBr.FindAllStringSubmatch(text, -1); len(mrBr) > 0 { for _, item := range mrBr { res = regexp.MustCompile(item[0]).ReplaceAllLiteralString(res, "\n") } } if mrHr := reHr.FindAllStringSubmatch(res, -1); len(mrHr) > 0 { for _, item := range mrHr { res = regexp.MustCompile(item[0]).ReplaceAllLiteralString(res, "\n") } } if mrP := reP.FindAllStringSubmatch(res, -1); len(mrP) > 0 { for _, item := range mrP { res = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(res, fmt.Sprintf("\n%s", item[1])) } } if mrFont := reFont.FindAllStringSubmatch(res, -1); len(mrFont) > 0 { for _, item := range mrFont { res = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(res, fmt.Sprintf("%s", item[2])) //ReplaceAllLiteral(res, []byte(fmt.Sprintf("#%s#%s#", item[1], item[2]))) } } if mrBold
reStrong = regexp.MustCompile("<strong.*?>(.*?)</strong>") reItalic = regexp.MustCompile("<i>((?s:.+?))</i>") reSpan = regexp.MustCompile("<span.*?>(.*?)</span>")
random_line_split
helpers.go
(text string, re *regexp.Regexp) (string, Coordinates) { var ( result = text mr = re.FindAllStringSubmatch(text, -1) coords = Coordinates{} ) if len(mr) > 0 { for _, item := range mr { lon, _ := strconv.ParseFloat(item[1], 64) lat, _ := strconv.ParseFloat(item[2], 64) if len(item) > 3 { coords = append(coords, Coordinate{Lat: lon, Lon: lat, OriginalString: item[3]}) } else { coords = append(coords, Coordinate{Lat: lon, Lon: lat, OriginalString: item[0]}) } result = regexp.MustCompile(item[0]).ReplaceAllLiteralString(result, "#coords#") } } return result, coords } // ExtractCoordinates extracts coordinates from the given text and returns the updated string // with replaced coordinates and the list of coordinates func ExtractCoordinates(text string) (string, Coordinates) { var ( // <a href="geo:49.976136, 36.267256">49.976136, 36.267256</a> geoHrefRe = regexp.MustCompile("<a.+?href=\"geo:(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})\">(.+?)</a>") // <a href="https://www.google.com.ua/maps/@50.0363257,36.2120039,19z" target="blank">50.036435 36.211914</a> hrefRe = regexp.MustCompile("<a.+?href=\"https?://.+?(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,}).*?\">(.+?)</a>") // 49.976136, 36.267256 numbersRe = regexp.MustCompile("(\\d{2}[.,]\\d{3,}),?\\s*(\\d{2}[.,]\\d{3,})") res = text coords = Coordinates{} tmpCoords Coordinates ) log.Print("[INFO] Extract coordinates from task text") for _, re := range []*regexp.Regexp{geoHrefRe, hrefRe, numbersRe} { res, tmpCoords = extractCoordinates(res, re) coords = append(coords, tmpCoords...) } for _, coord := range coords { res = strings.Replace(res, "#coords#", coord.OriginalString, 1) } if DEBUG { log.Printf("[DEBUG] Found %d coordinates", len(coords)) } return res, coords } func extractImages(text string, re *regexp.Regexp, caption string, start int) (string, Images) { var ( result = text mr = re.FindAllStringSubmatch(text, -1) images = Images{} ) if len(mr) > 0 { for i, item := range mr { images = append(images, Image{URL: item[1], Caption: fmt.Sprintf("%s #%d", caption, start+i)}) result = regexp.MustCompile(regexp.QuoteMeta(item[0])). ReplaceAllLiteralString(result, fmt.Sprintf("%s #%d", caption, start+i)) } } return result, images } // ExtractImages extracts images from the given text and returns the updated // version of the text and the list of images func ExtractImages(text string, caption string) (string, Images) { var ( reImg = regexp.MustCompile("<img.+?src=\"\\s*(https?://.+?)\\s*\".*?>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(https?://.+?\\.(jpg|png|bmp))\\\\?\".*?>(.*?)</a>") result = text images = Images{} tmpImages Images ) //log.Printf("Before image replacing: %s", text) log.Print("[INFO] Extract images from task text") for _, re := range []*regexp.Regexp{reImg, reA} { result, tmpImages = extractImages(result, re, caption, len(images)+1) images = append(images, tmpImages...) } if DEBUG { log.Printf("[DEBUG] Found %d images", len(images)) } return result, images } // ReplaceHTMLTags finds all html tags and removes them. Some tags like bold, italic are replaed with // makrkups for telegram func ReplaceHTMLTags(text string) string { var ( parser = html.NewTokenizer(strings.NewReader(text)) tagStack = stack.New() textToTag = map[int]string{} ) for { node := parser.Next() switch node { case html.ErrorToken: result := strings.Replace(textToTag[0], "&nbsp;", " ", -1) return result case html.TextToken: t := string(parser.Text()) textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], t}, "") case html.StartTagToken: tagName, hasAttr := parser.TagName() if string(tagName) == scriptTag { // We can skip script tags, as they are invisible for the user, but we can indicate that there are // scripts in the task. To skip tag, it is necessary to call Next() two times: // 1) returns TextToken with the script body // 2) returns EndTagToken for the closed script tag // Usually script tag doesn't have any neste tags, so this aproach should work log.Printf("[INFO] Skipping script tag") parser.Next() parser.Next() continue } tag := Tag{Tag: string(tagName), Attrs: map[string]string{}} if hasAttr { for { attr, val, moreAttr := parser.TagAttr() if DEBUG { log.Printf("[DEBUG] Found attr %s", attr) } tag.Attrs[string(attr)] = string(val) if !moreAttr { break } } } if DEBUG { log.Printf("[DEBUG] Found tag %q", tag) } tagStack.Push(tag) case html.EndTagToken: var ( addText string tagNo = tagStack.Len() tag = tagStack.Pop() closedTag, _ = parser.TagName() ) if tag.(Tag).Tag != string(closedTag) { log.Printf("[WARNING] Found closed tag %q but expected %q", closedTag, tag) continue } if DEBUG { log.Printf("[DEBUG] Found end of tag %q", closedTag) } switch tag.(Tag).Tag { case iTag: addText = fmt.Sprintf("_%s_", textToTag[tagNo]) case bTag, strongTag: addText = fmt.Sprintf("*%s*", textToTag[tagNo]) case aTag: // if strings.Compare(string(attr), "href") == 0 { addText = fmt.Sprintf("[%s](%s)", textToTag[tagNo], tag.(Tag).Attrs["href"]) // } default: addText = textToTag[tagNo] } textToTag[tagStack.Len()] = strings.Join([]string{textToTag[tagStack.Len()], addText}, "") delete(textToTag, tagNo) } } } // ReplaceCommonTags deprecated - should be removed!!! func ReplaceCommonTags(text string) string { log.Print("Replace html tags") var ( reBr = regexp.MustCompile("<br\\s*/?>") reHr = regexp.MustCompile("<hr.*?/?>") reP = regexp.MustCompile("<p>([^ ]+?)</p>") reBold = regexp.MustCompile("<b.*?/?>((?s:.*?))</b>") reStrong = regexp.MustCompile("<strong.*?>(.*?)</strong>") reItalic = regexp.MustCompile("<i>((?s:.+?))</i>") reSpan = regexp.MustCompile("<span.*?>(.*?)</span>") reCenter = regexp.MustCompile("<center>((?s:.*?))</center>") reFont = regexp.MustCompile("<font.+?color\\s*=\\\\?[\"«]?#?(\\w+)\\\\?[\"»]?.*?>((?s:.*?))</font>") reA = regexp.MustCompile("<a.+?href=\\\\?\"(.+?)\\\\?\".*?>(.+?)</a>") res = text ) res = strings.Replace(text, "_", "\\_", -1) if mrBr := reBr.FindAllStringSubmatch(text, -1); len(mrBr) > 0 { for _, item := range mrBr { res = regexp.MustCompile(item[0]).ReplaceAllLiteralString(res, "\n") } } if mrHr := reHr.FindAllStringSubmatch(res, -1); len(mrHr) > 0 {
extractCoordinates
identifier_name
trainPredictor.py
pathjoin = os.path.join pathexists = os.path.exists mdy = dtime.datetime.now().strftime('%m%d%y') product_type = 'interferogram' cache_dir = 'cached' train_folds = np.inf # inf = leave-one-out, otherwise k-fold cross validation train_state = 42 # random seed train_verbose = 0 train_jobs = -1 cv_type = 'loo' if train_folds==np.inf else '%d-fold'%train_folds cv_probs = True # record prediction probabilities in addition to labels scorefn = {} # map from name (e.g., mse) -> f(y_true,y_pred) scorefn['precision'] = lambda te,pr,ul: precision_score(te,pr,labels=ul) scorefn['recall'] = lambda te,pr,ul: recall_score(te,pr,labels=ul) errorfn = {} # map from name (e.g., diff) -> f(y_true,y_pred) errorfn['match'] = lambda y_true,y_pred: y_true==y_pred # GRID SEARCH PARAMS FOR PARAMETER TUNING ###################################### gridcv_folds = 2 # number of cross-validation folds per gridcv parameter gridcv_jobs = -1 # -1 = use all cores gridcv_verbose = 0 # verbosity level of model-tuning cross-validation output gridcv_score = 'roc_auc' # SKLEARN MODEL SPECIFICATIONS ################################################# ### Random Forest ############################################################## rf_trees = 500 rf_feats = np.linspace(0.1,1.0,5) rf_depth = [2,4,7,10,25] rf_jobs = 1 if gridcv_jobs == -1 else -1 # multiprocessing + RandomForest don't play nice rf_tuned = {'max_features':rf_feats,'max_depth':rf_depth} rf_defaults = { 'n_estimators': rf_trees,'max_features':'sqrt','n_jobs':rf_jobs, 'verbose':train_verbose,'random_state':train_state, 'criterion':'gini','class_weight':'balanced_subsample' } ### XGBoost #################################################################### xgb_depth = [3,4,5,10,25] xgb_subsample = np.linspace(0.1,1,5) xgb_default = { 'n_estimators':rf_trees,'max_delta_step':1,'learning_rate':0.1, 'objective':'binary:logistic','max_depth':3,'subsample':0.5, 'colsample_bytree':1,'subsample':1,'silent':(not train_verbose), 'seed':train_state,'nthread':train_jobs } xgb_tuned = {'learning_rate':[0.001,0.01,0.05,0.1,0.25,0.33], 'max_depth':xgb_depth,'subsample':xgb_subsample} def loadjson(jsonfile): with open(jsonfile,'r') as fid: return json.load(fid) def dumpjson(objdict,jsonfile): with open(jsonfile,'w') as fid: return json.dump(fid,objdict) def url2pid(url): """ url2pid(url): convert url to product id Arguments: - url: url to convert Keyword Arguments: None Returns: - product id for url """ if url.endswith('/'): url = url[:-1] urlsplit = url.split('/') return (urlsplit[-2] + '_' + urlsplit[-1]).replace('__','_') def url2featid(url,product_type): """ url2pid(url): convert url to feature id Arguments: - url: url to convert Keyword Arguments: None Returns: - feature id for url """ return url.replace(product_type,'features').replace('features__','features_'+product_type+'__') def fdict2vec(featdict,clfinputs): ''' extract feature vector from dict given classifier parameters specifying which features to use ''' fvec = [] try: featspec = clfinputs['features'] featorder = featspec['feature_order'] featdims = featspec['feature_dims'] cohthr = featspec['cohthr10'] featscoh = featdict['%d'%cohthr] for fid,fdim in zip(featorder,featdims): flist = featscoh[fid] if not isinstance(flist,list): flist = [flist] assert(len(flist) == fdim) fvec.extend(flist) except Exception: pass return fvec def curlProductMeta(prod_url,verbose=False,remove=True): """ curlProductMeta(prod_url,verbose=False) Arguments: - prod_url: product url Keyword Arguments: - verbose: verbose output (default=False) Returns: metadata dict from product .met.json """ if prod_url.endswith('/'): prod_url = prod_url[:-1] prod_json = url2pid(prod_url) + '.met.json' try: uu = UrlUtils() silentoutput = ' ' if verbose else ' --silent ' userstr = uu.dav_u + ':' + uu.dav_p command = 'curl' + silentoutput + '-k -f -u' + userstr + ' -O ' + pathjoin(prod_url,prod_json) os.system(command) except Exception: return {} if not pathexists(prod_json): return {} meta = loadjson(prod_json) if remove: os.remove(prod_json) return meta def getFeatures(url,clfinputs,product_type='interferogram'): ''' retrieves feature vector for the given product url, provided clfinputs ''' featurl = url2featid(url,product_type) featdict = curlProductMeta(featurl) fvec = fdict2vec(featdict,clfinputs) return fvec def loadQuery(querymeta,queryoptions=[],queryoutfile=None,cache=False): ''' builds/posts the faceted search query specified in querymeta and dumps the result to queryoutfile. if queryoutfile already exists, the query is loaded from disk rather than executed. ''' if not cache or not pathexists(queryoutfile): print('executing faceted search query...') from utils.queryBuilder import postQuery, buildQuery from utils.contextUtils import toContext ret,status = postQuery(buildQuery(querymeta,queryoptions)) if cache and status: # only dump the query if caching enabled and postQuery succeeds with open(queryoutfile,'wb') as fid: pickle.dump(ret,fid) elif cache: print('loading cached query from %s...'%queryoutfile) with open(queryoutfile,'rb') as fid: ret = pickle.load(fid) print('query returned %d products'%len(ret)) return ret def loadClassmap(cmapjson): """ loadClassmap(cmapjson) - loads classmap file, substitutes '_', for '-' as necessary Arguments: - cmapjson: classmap .json file Keyword Arguments: None Returns: classmap with substitutions """ initialmap = loadjson(cmapjson) classmap = initialmap.copy() # substitute '-' with '_' (for user-tagged typos) tags = list(initialmap.keys()) for tag in tags: if '-' in tag: classmap[tag.replace('-','_')] = classmap[tag] return classmap def loadPredictorSpec(clfjson): """ loadPredictorSpec(clfjson) Arguments: - clfjson: json file specifying classifier parameters Keyword Arguments: None Returns: dict containing classifier parameters, including (but not limited to): - classmap: classmap to map user tags to labels - features: dict containing information about features used to train classifier """ clfspec = loadjson(clfjson) clfspec['classmap'] = loadClassmap(clfspec["classmap_file"]) clfspec['features'] = loadjson(clfspec["feat_file"]) return clfspec def dumpPredictorSpec(inputs): clfspec = {} clfspec['clf_file'] = inputs['clf_name']+'.pkl' for key in ['clf_type','classmap','feat_file']: clfspec[key] = inputs[key] json.dump(clfspec,inputs['clf_name']+'.json') def PredictorSpec(inputjson): clfspec['clf_file'] = inputs['clf_file'] clfspec['classmap'] = inputs["classmap_file"] clfspec['features'] = inputs("feat_file") def usertags2label(usertags,classmap): ''' return dictionary of matched (tag,label) pairs in classmap for all tags returns {} if none of the tags are present in classmap ''' labelmap = {} for tag in usertags: tag = tag.strip() for k,v in list(classmap.items()): if tag.count(k): labelmap[tag] = v return labelmap def queryAllTags(taglist,cache=False): ''' return
print(process,exitv,message)
identifier_body
trainPredictor.py
ProductMeta(prod_url,verbose=False,remove=True): """ curlProductMeta(prod_url,verbose=False) Arguments: - prod_url: product url Keyword Arguments: - verbose: verbose output (default=False) Returns: metadata dict from product .met.json """ if prod_url.endswith('/'): prod_url = prod_url[:-1] prod_json = url2pid(prod_url) + '.met.json' try: uu = UrlUtils() silentoutput = ' ' if verbose else ' --silent ' userstr = uu.dav_u + ':' + uu.dav_p command = 'curl' + silentoutput + '-k -f -u' + userstr + ' -O ' + pathjoin(prod_url,prod_json) os.system(command) except Exception: return {} if not pathexists(prod_json): return {} meta = loadjson(prod_json) if remove: os.remove(prod_json) return meta def getFeatures(url,clfinputs,product_type='interferogram'): ''' retrieves feature vector for the given product url, provided clfinputs ''' featurl = url2featid(url,product_type) featdict = curlProductMeta(featurl) fvec = fdict2vec(featdict,clfinputs) return fvec def loadQuery(querymeta,queryoptions=[],queryoutfile=None,cache=False): ''' builds/posts the faceted search query specified in querymeta and dumps the result to queryoutfile. if queryoutfile already exists, the query is loaded from disk rather than executed. ''' if not cache or not pathexists(queryoutfile): print('executing faceted search query...') from utils.queryBuilder import postQuery, buildQuery from utils.contextUtils import toContext ret,status = postQuery(buildQuery(querymeta,queryoptions)) if cache and status: # only dump the query if caching enabled and postQuery succeeds with open(queryoutfile,'wb') as fid: pickle.dump(ret,fid) elif cache: print('loading cached query from %s...'%queryoutfile) with open(queryoutfile,'rb') as fid: ret = pickle.load(fid) print('query returned %d products'%len(ret)) return ret def loadClassmap(cmapjson): """ loadClassmap(cmapjson) - loads classmap file, substitutes '_', for '-' as necessary Arguments: - cmapjson: classmap .json file Keyword Arguments: None Returns: classmap with substitutions """ initialmap = loadjson(cmapjson) classmap = initialmap.copy() # substitute '-' with '_' (for user-tagged typos) tags = list(initialmap.keys()) for tag in tags: if '-' in tag: classmap[tag.replace('-','_')] = classmap[tag] return classmap def loadPredictorSpec(clfjson): """ loadPredictorSpec(clfjson) Arguments: - clfjson: json file specifying classifier parameters Keyword Arguments: None Returns: dict containing classifier parameters, including (but not limited to): - classmap: classmap to map user tags to labels - features: dict containing information about features used to train classifier """ clfspec = loadjson(clfjson) clfspec['classmap'] = loadClassmap(clfspec["classmap_file"]) clfspec['features'] = loadjson(clfspec["feat_file"]) return clfspec def dumpPredictorSpec(inputs): clfspec = {} clfspec['clf_file'] = inputs['clf_name']+'.pkl' for key in ['clf_type','classmap','feat_file']: clfspec[key] = inputs[key] json.dump(clfspec,inputs['clf_name']+'.json') def PredictorSpec(inputjson): clfspec['clf_file'] = inputs['clf_file'] clfspec['classmap'] = inputs["classmap_file"] clfspec['features'] = inputs("feat_file") def
(usertags,classmap): ''' return dictionary of matched (tag,label) pairs in classmap for all tags returns {} if none of the tags are present in classmap ''' labelmap = {} for tag in usertags: tag = tag.strip() for k,v in list(classmap.items()): if tag.count(k): labelmap[tag] = v return labelmap def queryAllTags(taglist,cache=False): ''' return all urls with user tags present in taglist ''' tagpkl = pathjoin(cache_dir,"usertags.pkl") tagquery = {'dataset_type':product_type,'tags':taglist} querylist = loadQuery(tagquery,cache=cache,queryoutfile=tagpkl) querydict = {} for product in querylist: purl = product['url'] querydict[purl] = product return querydict def collectUrlTags(urllist,querymeta={}): """ collectUrlTags(urllist,querymeta={}) collects user tags for a list of urls Arguments: - urllist: list of urls Keyword Arguments: - querymeta: (default={}) Returns: dict keyed on product id containing - url: input url - user_tags: tags for input url """ tagdict = {} nurl = len(urllist) for i,url in enumerate(urllist): if url in querymeta: # use the query input if possible meta = querymeta[url] else: # otherwise retrieve product metadata via curl meta = curlProductMeta(url) tagdict[url2pid(url)] = {'url':url,'user_tags':meta.get('user_tags',[])} return tagdict def collectTrainingData(urls,clfinputs,cache=False): ''' construct matrix of training samples X with labels y by intersecting the set of IGMs with extracted features (featquery) with the set of tagged IGMs (taggedquery) Keep only IGMs with tags present in classmap, and select/validate features according to the parameters in clfinputs. Returns: dict containing: - tags: list of user tags used to select training samples - X, y: training samples, labels - traintags: tags for each training sample - trainurls: url for each training sample - skiplist: list of urls which could not be retrieved due to errors - errors: list of error strings for each url in skiplist ''' classmap = clfinputs['classmap'] tags = sorted(list(classmap.keys())) traindatpkl = pathjoin(cache_dir,"traindat.pkl") if cache and pathexists(traindatpkl): print('loading training data from %s...'%traindatpkl) with open(traindatpkl,'rb') as fid: ret = pickle.load(fid) # make sure the set of tags match if all([ret['tags'][i] == tags[i] for i in range(len(tags))]): return ret print("querying %d tags"%len(tags)) querymeta = queryAllTags(tags,cache=cache) if len(urls)==0: print('no URLs provided, training using all tags in classmap') # construct/run query to get metadata for all products with given tags urls = list(querymeta.keys()) elif isinstance(urls,str): urls = [urls] tagdict = collectUrlTags(urls,querymeta=querymeta) ntagged = len(tagdict) X,y = [],[] traintags,trainurls = [],[] errors,skiplist = [],[] widgets = ['collecting features for %d products'%ntagged, Percentage(), ' ', Bar('='), ' ', ETA()] pbar = ProgressBar(widgets=widgets, maxval=ntagged).start() for i,pid in enumerate(tagdict): tdict = tagdict[pid] turl,ttags = tdict['url'],tdict['user_tags'] taglabel = usertags2label(ttags,classmap) if len(taglabel) == 0: continue fvec = getFeatures(turl,clfinputs) if len(fvec)==0: errmsg = "error collecting features for product %s (skipped)"%pid errors.append(errmsg) skiplist.append(turl) continue pidtags,pidlabs = list(taglabel.keys()),list(taglabel.values()) if len(pidtags) == 1: X.append(fvec) y.append(pidlabs[0]) traintags.append(pidtags[0]) trainurls.append(turl) elif len(pidtags) > 1: ulab = np.unique(pidlabs) if len(ulab) == 1: X.append(fvec) y.append(pidlabs[0]) traintags.append(pidtags[0]) trainurls.append(turl) else: errmsg = "conflicting tags (%s) for product %s, skipped"%(pidtags,pid) errors.append(errmsg) skiplist.append(turl) p
usertags2label
identifier_name
trainPredictor.py
search query...') from utils.queryBuilder import postQuery, buildQuery from utils.contextUtils import toContext ret,status = postQuery(buildQuery(querymeta,queryoptions)) if cache and status: # only dump the query if caching enabled and postQuery succeeds with open(queryoutfile,'wb') as fid: pickle.dump(ret,fid) elif cache: print('loading cached query from %s...'%queryoutfile) with open(queryoutfile,'rb') as fid: ret = pickle.load(fid) print('query returned %d products'%len(ret)) return ret def loadClassmap(cmapjson): """ loadClassmap(cmapjson) - loads classmap file, substitutes '_', for '-' as necessary Arguments: - cmapjson: classmap .json file Keyword Arguments: None Returns: classmap with substitutions """ initialmap = loadjson(cmapjson) classmap = initialmap.copy() # substitute '-' with '_' (for user-tagged typos) tags = list(initialmap.keys()) for tag in tags: if '-' in tag: classmap[tag.replace('-','_')] = classmap[tag] return classmap def loadPredictorSpec(clfjson): """ loadPredictorSpec(clfjson) Arguments: - clfjson: json file specifying classifier parameters Keyword Arguments: None Returns: dict containing classifier parameters, including (but not limited to): - classmap: classmap to map user tags to labels - features: dict containing information about features used to train classifier """ clfspec = loadjson(clfjson) clfspec['classmap'] = loadClassmap(clfspec["classmap_file"]) clfspec['features'] = loadjson(clfspec["feat_file"]) return clfspec def dumpPredictorSpec(inputs): clfspec = {} clfspec['clf_file'] = inputs['clf_name']+'.pkl' for key in ['clf_type','classmap','feat_file']: clfspec[key] = inputs[key] json.dump(clfspec,inputs['clf_name']+'.json') def PredictorSpec(inputjson): clfspec['clf_file'] = inputs['clf_file'] clfspec['classmap'] = inputs["classmap_file"] clfspec['features'] = inputs("feat_file") def usertags2label(usertags,classmap): ''' return dictionary of matched (tag,label) pairs in classmap for all tags returns {} if none of the tags are present in classmap ''' labelmap = {} for tag in usertags: tag = tag.strip() for k,v in list(classmap.items()): if tag.count(k): labelmap[tag] = v return labelmap def queryAllTags(taglist,cache=False): ''' return all urls with user tags present in taglist ''' tagpkl = pathjoin(cache_dir,"usertags.pkl") tagquery = {'dataset_type':product_type,'tags':taglist} querylist = loadQuery(tagquery,cache=cache,queryoutfile=tagpkl) querydict = {} for product in querylist: purl = product['url'] querydict[purl] = product return querydict def collectUrlTags(urllist,querymeta={}): """ collectUrlTags(urllist,querymeta={}) collects user tags for a list of urls Arguments: - urllist: list of urls Keyword Arguments: - querymeta: (default={}) Returns: dict keyed on product id containing - url: input url - user_tags: tags for input url """ tagdict = {} nurl = len(urllist) for i,url in enumerate(urllist): if url in querymeta: # use the query input if possible meta = querymeta[url] else: # otherwise retrieve product metadata via curl meta = curlProductMeta(url) tagdict[url2pid(url)] = {'url':url,'user_tags':meta.get('user_tags',[])} return tagdict def collectTrainingData(urls,clfinputs,cache=False): ''' construct matrix of training samples X with labels y by intersecting the set of IGMs with extracted features (featquery) with the set of tagged IGMs (taggedquery) Keep only IGMs with tags present in classmap, and select/validate features according to the parameters in clfinputs. Returns: dict containing: - tags: list of user tags used to select training samples - X, y: training samples, labels - traintags: tags for each training sample - trainurls: url for each training sample - skiplist: list of urls which could not be retrieved due to errors - errors: list of error strings for each url in skiplist ''' classmap = clfinputs['classmap'] tags = sorted(list(classmap.keys())) traindatpkl = pathjoin(cache_dir,"traindat.pkl") if cache and pathexists(traindatpkl): print('loading training data from %s...'%traindatpkl) with open(traindatpkl,'rb') as fid: ret = pickle.load(fid) # make sure the set of tags match if all([ret['tags'][i] == tags[i] for i in range(len(tags))]): return ret print("querying %d tags"%len(tags)) querymeta = queryAllTags(tags,cache=cache) if len(urls)==0: print('no URLs provided, training using all tags in classmap') # construct/run query to get metadata for all products with given tags urls = list(querymeta.keys()) elif isinstance(urls,str): urls = [urls] tagdict = collectUrlTags(urls,querymeta=querymeta) ntagged = len(tagdict) X,y = [],[] traintags,trainurls = [],[] errors,skiplist = [],[] widgets = ['collecting features for %d products'%ntagged, Percentage(), ' ', Bar('='), ' ', ETA()] pbar = ProgressBar(widgets=widgets, maxval=ntagged).start() for i,pid in enumerate(tagdict): tdict = tagdict[pid] turl,ttags = tdict['url'],tdict['user_tags'] taglabel = usertags2label(ttags,classmap) if len(taglabel) == 0: continue fvec = getFeatures(turl,clfinputs) if len(fvec)==0: errmsg = "error collecting features for product %s (skipped)"%pid errors.append(errmsg) skiplist.append(turl) continue pidtags,pidlabs = list(taglabel.keys()),list(taglabel.values()) if len(pidtags) == 1: X.append(fvec) y.append(pidlabs[0]) traintags.append(pidtags[0]) trainurls.append(turl) elif len(pidtags) > 1: ulab = np.unique(pidlabs) if len(ulab) == 1: X.append(fvec) y.append(pidlabs[0]) traintags.append(pidtags[0]) trainurls.append(turl) else: errmsg = "conflicting tags (%s) for product %s, skipped"%(pidtags,pid) errors.append(errmsg) skiplist.append(turl) pbar.update(i) pbar.finish() # sort products by product url to ensure identical ordering of X,y sorti = np.argsort(trainurls) print('collected', len(sorti), 'training samples (skipped %d)'%len(skiplist)) X,y = np.array(X)[sorti,:],np.array(y)[sorti] traintags,trainurls = np.array(traintags)[sorti],np.array(trainurls)[sorti] ret = {'tags':tags,'X':X,'y':y,'traintags':traintags,'trainurls':trainurls, 'skiplist':skiplist,'errors':errors} if cache: with open(traindatpkl,'wb') as fid: pickle.dump(ret,fid) print('saved training data to %s'%traindatpkl) return ret def train(X_train,y_train,clfinputs,**kwargs): """ train(X_train,y_train,clfinputs,**kwargs) train a classifier with parameter tuning via gridsearchcv Arguments: - X_train: training data (N x n matrix) - y_train: training labels (N x 1 vector) - clfinputs: classifier spec Keyword Arguments: None Returns: - clf: tuned classifier - cv: cross validation struct used to tune classifier """ uy = np.unique(y_train) if len(uy) != 2: print('error: need 2 classes for classification!') return None,None model_id = clfinputs['clf_type'] if model_id == 'rf':
model_clf = RandomForestClassifier(**rf_defaults) model_tuned = [rf_tuned]
conditional_block
trainPredictor.py
return url.replace(product_type,'features').replace('features__','features_'+product_type+'__') def fdict2vec(featdict,clfinputs): ''' extract feature vector from dict given classifier parameters specifying which features to use ''' fvec = [] try: featspec = clfinputs['features'] featorder = featspec['feature_order'] featdims = featspec['feature_dims'] cohthr = featspec['cohthr10'] featscoh = featdict['%d'%cohthr] for fid,fdim in zip(featorder,featdims): flist = featscoh[fid] if not isinstance(flist,list): flist = [flist] assert(len(flist) == fdim) fvec.extend(flist) except Exception: pass return fvec def curlProductMeta(prod_url,verbose=False,remove=True): """ curlProductMeta(prod_url,verbose=False) Arguments: - prod_url: product url Keyword Arguments: - verbose: verbose output (default=False) Returns: metadata dict from product .met.json """ if prod_url.endswith('/'): prod_url = prod_url[:-1] prod_json = url2pid(prod_url) + '.met.json' try: uu = UrlUtils() silentoutput = ' ' if verbose else ' --silent ' userstr = uu.dav_u + ':' + uu.dav_p command = 'curl' + silentoutput + '-k -f -u' + userstr + ' -O ' + pathjoin(prod_url,prod_json) os.system(command) except Exception: return {} if not pathexists(prod_json): return {} meta = loadjson(prod_json) if remove: os.remove(prod_json) return meta def getFeatures(url,clfinputs,product_type='interferogram'): ''' retrieves feature vector for the given product url, provided clfinputs ''' featurl = url2featid(url,product_type) featdict = curlProductMeta(featurl) fvec = fdict2vec(featdict,clfinputs) return fvec def loadQuery(querymeta,queryoptions=[],queryoutfile=None,cache=False): ''' builds/posts the faceted search query specified in querymeta and dumps the result to queryoutfile. if queryoutfile already exists, the query is loaded from disk rather than executed. ''' if not cache or not pathexists(queryoutfile): print('executing faceted search query...') from utils.queryBuilder import postQuery, buildQuery from utils.contextUtils import toContext ret,status = postQuery(buildQuery(querymeta,queryoptions)) if cache and status: # only dump the query if caching enabled and postQuery succeeds with open(queryoutfile,'wb') as fid: pickle.dump(ret,fid) elif cache: print('loading cached query from %s...'%queryoutfile) with open(queryoutfile,'rb') as fid: ret = pickle.load(fid) print('query returned %d products'%len(ret)) return ret def loadClassmap(cmapjson): """ loadClassmap(cmapjson) - loads classmap file, substitutes '_', for '-' as necessary Arguments: - cmapjson: classmap .json file Keyword Arguments: None Returns: classmap with substitutions """ initialmap = loadjson(cmapjson) classmap = initialmap.copy() # substitute '-' with '_' (for user-tagged typos) tags = list(initialmap.keys()) for tag in tags: if '-' in tag: classmap[tag.replace('-','_')] = classmap[tag] return classmap def loadPredictorSpec(clfjson): """ loadPredictorSpec(clfjson) Arguments: - clfjson: json file specifying classifier parameters Keyword Arguments: None Returns: dict containing classifier parameters, including (but not limited to): - classmap: classmap to map user tags to labels - features: dict containing information about features used to train classifier """ clfspec = loadjson(clfjson) clfspec['classmap'] = loadClassmap(clfspec["classmap_file"]) clfspec['features'] = loadjson(clfspec["feat_file"]) return clfspec def dumpPredictorSpec(inputs): clfspec = {} clfspec['clf_file'] = inputs['clf_name']+'.pkl' for key in ['clf_type','classmap','feat_file']: clfspec[key] = inputs[key] json.dump(clfspec,inputs['clf_name']+'.json') def PredictorSpec(inputjson): clfspec['clf_file'] = inputs['clf_file'] clfspec['classmap'] = inputs["classmap_file"] clfspec['features'] = inputs("feat_file") def usertags2label(usertags,classmap): ''' return dictionary of matched (tag,label) pairs in classmap for all tags returns {} if none of the tags are present in classmap ''' labelmap = {} for tag in usertags: tag = tag.strip() for k,v in list(classmap.items()): if tag.count(k): labelmap[tag] = v return labelmap def queryAllTags(taglist,cache=False): ''' return all urls with user tags present in taglist ''' tagpkl = pathjoin(cache_dir,"usertags.pkl") tagquery = {'dataset_type':product_type,'tags':taglist} querylist = loadQuery(tagquery,cache=cache,queryoutfile=tagpkl) querydict = {} for product in querylist: purl = product['url'] querydict[purl] = product return querydict def collectUrlTags(urllist,querymeta={}): """ collectUrlTags(urllist,querymeta={}) collects user tags for a list of urls Arguments: - urllist: list of urls Keyword Arguments: - querymeta: (default={}) Returns: dict keyed on product id containing - url: input url - user_tags: tags for input url """ tagdict = {} nurl = len(urllist) for i,url in enumerate(urllist): if url in querymeta: # use the query input if possible meta = querymeta[url] else: # otherwise retrieve product metadata via curl meta = curlProductMeta(url) tagdict[url2pid(url)] = {'url':url,'user_tags':meta.get('user_tags',[])} return tagdict def collectTrainingData(urls,clfinputs,cache=False): ''' construct matrix of training samples X with labels y by intersecting the set of IGMs with extracted features (featquery) with the set of tagged IGMs (taggedquery) Keep only IGMs with tags present in classmap, and select/validate features according to the parameters in clfinputs. Returns: dict containing: - tags: list of user tags used to select training samples - X, y: training samples, labels - traintags: tags for each training sample - trainurls: url for each training sample - skiplist: list of urls which could not be retrieved due to errors - errors: list of error strings for each url in skiplist ''' classmap = clfinputs['classmap'] tags = sorted(list(classmap.keys())) traindatpkl = pathjoin(cache_dir,"traindat.pkl") if cache and pathexists(traindatpkl): print('loading training data from %s...'%traindatpkl) with open(traindatpkl,'rb') as fid: ret = pickle.load(fid) # make sure the set of tags match if all([ret['tags'][i] == tags[i] for i in range(len(tags))]): return ret print("querying %d tags"%len(tags)) querymeta = queryAllTags(tags,cache=cache) if len(urls)==0: print('no URLs provided, training using all tags in classmap') # construct/run query to get metadata for all products with given tags urls = list(querymeta.keys()) elif isinstance(urls,str): urls = [urls] tagdict = collectUrlTags(urls,querymeta=querymeta) ntagged = len(tagdict) X,y = [],[] traintags,trainurls = [],[] errors,skiplist = [],[] widgets = ['collecting features for %d products'%ntagged, Percentage(), ' ', Bar('='), ' ', ETA()] pbar = ProgressBar(widgets=widgets, maxval=ntagged).start() for i,pid in enumerate(tagdict): tdict = tagdict[pid] turl,ttags = tdict['url'],tdict['user_tags'] taglabel = usertags2label(ttags,classmap) if len(taglabel) == 0: continue fvec = getFeatures(turl,clfinputs)
- feature id for url """
random_line_split
photcalibration.py
::-1] ps1colorterms['i'] = [+0.01170, -0.00400, +0.00066, -0.00058][::-1] ps1colorterms['z'] = [-0.01062, +0.07529, -0.03592, +0.00890][::-1] def __init__(self, basedir): self.basedir = basedir self.skytable = None def PS1toSDSS(self, table): """ Modify table in situ from PS1 to SDSS, requires column names compatible with ps1colorterms definition. :param table: :return: modified table. """ if table is not None: pscolor = table['g'] - table['i'] for filter in self.ps1colorterms: colorcorrection = np.polyval(self.ps1colorterms[filter], pscolor) table[filter] -= colorcorrection return table def isInCatalogFootprint(self, ra, dec): """ Verify if image is in catalog footprint. TODO: Account for image field of view """ # PanSTARRS has valid entries for DEc > - 30 degrees return dec >= -30.0 def get_reference_catalog(self, ra, dec, radius, overwrite_select=False): """ Read i fits table from local catalog copy. Concatenate tables columns from different fits tables for full coverage. """ # A lot of safeguarding boiler plate to ensure catalog files are valid. if (self.basedir is None) or (not os.path.isdir(self.basedir)): _logger.error("Unable to find reference catalog: %s" % (str(self.basedir))) return None # Load the SkyTable so we know in what files to look for the catalog" _logger.debug("Using catalog found in %s" % (self.basedir)) skytable_filename = "%s/SkyTable.fits" % (self.basedir) if (not os.path.isfile(skytable_filename)): _logger.fatal("Unable to find catalog index file in %s!" % (self.basedir)) return None # Read in the master index hdu skytable_hdu = fits.open(skytable_filename) skytable = skytable_hdu['SKY_REGION'].data # Select entries that match our list # print ra, dec, radius, type(ra), type(dec), type(radius) # logger.debug("# Searching for stars within %.1f degress around %f , %f ..." % (radius, ra, dec)) if (not radius == None and radius > 0): min_dec = dec - radius max_dec = dec + radius min_ra = ra - radius / math.cos(math.radians(dec)) max_ra = ra + radius / math.cos(math.radians(dec)) else: min_dec, max_dec = dec[0], dec[1] min_ra, max_ra = ra[0], ra[1] _logger.debug("Querying catalog: Ra=%f...%f Dec=%f...%f" % (min_ra, max_ra, min_dec, max_dec)) if (max_ra > 360.): # This wraps around the high end, shift all ra values by -180 # Now all search RAs are ok and around the 180, next also move the catalog values selected = skytable['R_MIN'] < 180 skytable['R_MAX'][selected] += 360 skytable['R_MIN'][selected] += 360 if (min_ra < 0): # Wrap around at the low end selected = skytable['R_MAX'] > 180 skytable['R_MAX'][selected] -= 360 skytable['R_MIN'][selected] -= 360 _logger.debug("# Search radius: RA=%.1f ... %.1f DEC=%.1f ... %.1f" % (min_ra, max_ra, min_dec, max_dec)) try: needed_catalogs = (skytable['PARENT'] > 0) & (skytable['PARENT'] < 25) & \ (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) except KeyError: # try without the PARENT field needed_catalogs = (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) # print skytable[needed_catalogs] files_to_read = skytable['NAME'][needed_catalogs] files_to_read = [f.strip() for f in files_to_read] _logger.debug(files_to_read) skytable_hdu.close() # Warning: might erase the loaded data, might need to copy array! # Now quickly go over the list and take care of all filenames that still have a 0x00 in them for i in range(len(files_to_read)): found_at = files_to_read[i].find('\0') if (found_at > 0): files_to_read[i] = files_to_read[i][:found_at] # Load all frames, one by one, and select all stars in the valid range. # Then add them to the catalog with RAs and DECs full_catalog = None # numpy.zeros(shape=(0,6)) catalog_filenames = [] # Start iterating though catalogs for catalogname in files_to_read: catalogfile = "%s/%s" % (self.basedir, catalogname) # print catalogfile if (not os.path.isfile(catalogfile)): # not a file, try adding .fits to the end of the filename if (os.path.isfile(catalogfile + ".fits")): catalogfile += ".fits" else: # neither option (w/ or w/o .fits added is a file) _logger.warning( "Catalog file (%s) not found (base-dir: %s)" % (os.path.abspath(catalogfile), self.basedir)) continue try: hdu_cat = fits.open(catalogfile) except: _logger.warning("Unable to open catalog file %s" % (catalogfile)) continue catalog_filenames.append(catalogfile) _logger.debug("Adding %s to list of catalog files being used" % (catalogfile)) # read table into a nd-array buffer cat_full = hdu_cat[1].data hdu_cat.close() # Read the RA and DEC values cat_ra = cat_full['RA'] cat_dec = cat_full['DEC'] # To select the right region, shift a temporary catalog cat_ra_shifted = cat_ra if (max_ra > 360.): cat_ra_shifted[cat_ra < 180] += 360 elif (min_ra < 0): cat_ra_shifted[cat_ra > 180] -= 360 select_from_cat = (cat_ra_shifted > min_ra) & (cat_ra_shifted < max_ra) & (cat_dec > min_dec) & ( cat_dec < max_dec) array_to_add = cat_full[select_from_cat] _logger.debug("Read %d sources from %s" % (array_to_add.shape[0], catalogname)) if (full_catalog is None): full_catalog = array_to_add else: full_catalog = np.append(full_catalog, array_to_add, axis=0) # print photom_grizy[:3,:] if (full_catalog is None): _logger.warning("No stars found in area %s, %s from catalog %s" % ( str(ra), str(dec), # ra[0], ra[1], dec[0], dec[1], self.basedir)) else: _logger.debug( "Read a total of %d stars from %d catalogs!" % (full_catalog.shape[0], len(files_to_read))) self.PS1toSDSS(full_catalog) return full_catalog #### Wrapper routines to use photometric zeropointing stand-alone def crawlDirectory(directory, db, args):
search = "%s/*-[es][19]1.fits.fz" % (directory) inputlist = glob.glob(search) initialsize = len (inputlist) rejects = [] if not args.redo: for image in inputlist: if db.exists(image): rejects.append (image) for r in rejects: inputlist.remove (r) print ("Found %d files intially, but cleaned %d already measured images. Starting analysis of %d files" % (initialsize, len(rejects), len(inputlist))) photzpStage = PhotCalib(args.ps1dir) for image in inputlist: image = image.rstrip()
identifier_body
photcalibration.py
, 'defaultZP': 0.0} FILTERMAPPING['rp'] = {'refMag': 'r', 'colorTerm': 0.0, 'airmassTerm': 0.12, 'defaultZP': 0.0} FILTERMAPPING['ip'] = {'refMag': 'i', 'colorTerm': 0.0, 'airmassTerm': 0.08, 'defaultZP': 0.0} FILTERMAPPING['zp'] = {'refMag': 'z', 'colorTerm': 0.0, 'airmassTerm': 0.05, 'defaultZP': 0.0} ### PS to SDSS color transformations according to Finkbeiner 2016 ### http://iopscience.iop.org/article/10.3847/0004-637X/822/2/66/meta#apj522061s2-4 Table 2 ### Note that this transformation is valid for stars only. For the purpose of photometric ### calibration, it is desirable to select point sources only from the input catalog. ## Why reverse the order of the color term entries? Data are entered in the order as they are ## shown in paper. Reverse after the fact to avoid confusion when looking at paper ps1colorterms = {} ps1colorterms['g'] = [-0.01808, -0.13595, +0.01941, -0.00183][::-1] ps1colorterms['r'] = [-0.01836, -0.03577, +0.02612, -0.00558][::-1] ps1colorterms['i'] = [+0.01170, -0.00400, +0.00066, -0.00058][::-1] ps1colorterms['z'] = [-0.01062, +0.07529, -0.03592, +0.00890][::-1] def __init__(self, basedir): self.basedir = basedir self.skytable = None def PS1toSDSS(self, table): """ Modify table in situ from PS1 to SDSS, requires column names compatible with ps1colorterms definition. :param table: :return: modified table. """ if table is not None: pscolor = table['g'] - table['i'] for filter in self.ps1colorterms: colorcorrection = np.polyval(self.ps1colorterms[filter], pscolor) table[filter] -= colorcorrection return table def isInCatalogFootprint(self, ra, dec): """ Verify if image is in catalog footprint. TODO: Account for image field of view """ # PanSTARRS has valid entries for DEc > - 30 degrees return dec >= -30.0 def get_reference_catalog(self, ra, dec, radius, overwrite_select=False): """ Read i fits table from local catalog copy. Concatenate tables columns from different fits tables for full coverage. """ # A lot of safeguarding boiler plate to ensure catalog files are valid. if (self.basedir is None) or (not os.path.isdir(self.basedir)): _logger.error("Unable to find reference catalog: %s" % (str(self.basedir))) return None # Load the SkyTable so we know in what files to look for the catalog" _logger.debug("Using catalog found in %s" % (self.basedir)) skytable_filename = "%s/SkyTable.fits" % (self.basedir) if (not os.path.isfile(skytable_filename)): _logger.fatal("Unable to find catalog index file in %s!" % (self.basedir)) return None # Read in the master index hdu skytable_hdu = fits.open(skytable_filename) skytable = skytable_hdu['SKY_REGION'].data # Select entries that match our list # print ra, dec, radius, type(ra), type(dec), type(radius) # logger.debug("# Searching for stars within %.1f degress around %f , %f ..." % (radius, ra, dec)) if (not radius == None and radius > 0): min_dec = dec - radius max_dec = dec + radius min_ra = ra - radius / math.cos(math.radians(dec)) max_ra = ra + radius / math.cos(math.radians(dec)) else: min_dec, max_dec = dec[0], dec[1] min_ra, max_ra = ra[0], ra[1] _logger.debug("Querying catalog: Ra=%f...%f Dec=%f...%f" % (min_ra, max_ra, min_dec, max_dec)) if (max_ra > 360.): # This wraps around the high end, shift all ra values by -180 # Now all search RAs are ok and around the 180, next also move the catalog values selected = skytable['R_MIN'] < 180 skytable['R_MAX'][selected] += 360 skytable['R_MIN'][selected] += 360 if (min_ra < 0): # Wrap around at the low end selected = skytable['R_MAX'] > 180 skytable['R_MAX'][selected] -= 360 skytable['R_MIN'][selected] -= 360 _logger.debug("# Search radius: RA=%.1f ... %.1f DEC=%.1f ... %.1f" % (min_ra, max_ra, min_dec, max_dec)) try: needed_catalogs = (skytable['PARENT'] > 0) & (skytable['PARENT'] < 25) & \ (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) except KeyError: # try without the PARENT field needed_catalogs = (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) # print skytable[needed_catalogs] files_to_read = skytable['NAME'][needed_catalogs] files_to_read = [f.strip() for f in files_to_read] _logger.debug(files_to_read) skytable_hdu.close() # Warning: might erase the loaded data, might need to copy array! # Now quickly go over the list and take care of all filenames that still have a 0x00 in them for i in range(len(files_to_read)): found_at = files_to_read[i].find('\0') if (found_at > 0): files_to_read[i] = files_to_read[i][:found_at] # Load all frames, one by one, and select all stars in the valid range. # Then add them to the catalog with RAs and DECs full_catalog = None # numpy.zeros(shape=(0,6)) catalog_filenames = [] # Start iterating though catalogs for catalogname in files_to_read: catalogfile = "%s/%s" % (self.basedir, catalogname) # print catalogfile if (not os.path.isfile(catalogfile)): # not a file, try adding .fits to the end of the filename if (os.path.isfile(catalogfile + ".fits")): catalogfile += ".fits" else: # neither option (w/ or w/o .fits added is a file) _logger.warning( "Catalog file (%s) not found (base-dir: %s)" % (os.path.abspath(catalogfile), self.basedir)) continue try: hdu_cat = fits.open(catalogfile) except: _logger.warning("Unable to open catalog file %s" % (catalogfile)) continue catalog_filenames.append(catalogfile) _logger.debug("Adding %s to list of catalog files being used" % (catalogfile)) # read table into a nd-array buffer cat_full = hdu_cat[1].data hdu_cat.close() # Read the RA and DEC values cat_ra = cat_full['RA'] cat_dec = cat_full['DEC'] # To select the right region, shift a temporary catalog cat_ra_shifted = cat_ra if (max_ra > 360.): cat_ra_shifted[cat_ra < 180] += 360 elif (min_ra < 0):
cat_ra_shifted[cat_ra > 180] -= 360
conditional_block
photcalibration.py
'] = [-0.01062, +0.07529, -0.03592, +0.00890][::-1] def __init__(self, basedir): self.basedir = basedir self.skytable = None def PS1toSDSS(self, table): """ Modify table in situ from PS1 to SDSS, requires column names compatible with ps1colorterms definition. :param table: :return: modified table. """ if table is not None: pscolor = table['g'] - table['i'] for filter in self.ps1colorterms: colorcorrection = np.polyval(self.ps1colorterms[filter], pscolor) table[filter] -= colorcorrection return table def isInCatalogFootprint(self, ra, dec): """ Verify if image is in catalog footprint. TODO: Account for image field of view """ # PanSTARRS has valid entries for DEc > - 30 degrees return dec >= -30.0 def get_reference_catalog(self, ra, dec, radius, overwrite_select=False): """ Read i fits table from local catalog copy. Concatenate tables columns from different fits tables for full coverage. """ # A lot of safeguarding boiler plate to ensure catalog files are valid. if (self.basedir is None) or (not os.path.isdir(self.basedir)): _logger.error("Unable to find reference catalog: %s" % (str(self.basedir))) return None # Load the SkyTable so we know in what files to look for the catalog" _logger.debug("Using catalog found in %s" % (self.basedir)) skytable_filename = "%s/SkyTable.fits" % (self.basedir) if (not os.path.isfile(skytable_filename)): _logger.fatal("Unable to find catalog index file in %s!" % (self.basedir)) return None # Read in the master index hdu skytable_hdu = fits.open(skytable_filename) skytable = skytable_hdu['SKY_REGION'].data # Select entries that match our list # print ra, dec, radius, type(ra), type(dec), type(radius) # logger.debug("# Searching for stars within %.1f degress around %f , %f ..." % (radius, ra, dec)) if (not radius == None and radius > 0): min_dec = dec - radius max_dec = dec + radius min_ra = ra - radius / math.cos(math.radians(dec)) max_ra = ra + radius / math.cos(math.radians(dec)) else: min_dec, max_dec = dec[0], dec[1] min_ra, max_ra = ra[0], ra[1] _logger.debug("Querying catalog: Ra=%f...%f Dec=%f...%f" % (min_ra, max_ra, min_dec, max_dec)) if (max_ra > 360.): # This wraps around the high end, shift all ra values by -180 # Now all search RAs are ok and around the 180, next also move the catalog values selected = skytable['R_MIN'] < 180 skytable['R_MAX'][selected] += 360 skytable['R_MIN'][selected] += 360 if (min_ra < 0): # Wrap around at the low end selected = skytable['R_MAX'] > 180 skytable['R_MAX'][selected] -= 360 skytable['R_MIN'][selected] -= 360 _logger.debug("# Search radius: RA=%.1f ... %.1f DEC=%.1f ... %.1f" % (min_ra, max_ra, min_dec, max_dec)) try: needed_catalogs = (skytable['PARENT'] > 0) & (skytable['PARENT'] < 25) & \ (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) except KeyError: # try without the PARENT field needed_catalogs = (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) # print skytable[needed_catalogs] files_to_read = skytable['NAME'][needed_catalogs] files_to_read = [f.strip() for f in files_to_read] _logger.debug(files_to_read) skytable_hdu.close() # Warning: might erase the loaded data, might need to copy array! # Now quickly go over the list and take care of all filenames that still have a 0x00 in them for i in range(len(files_to_read)): found_at = files_to_read[i].find('\0') if (found_at > 0): files_to_read[i] = files_to_read[i][:found_at] # Load all frames, one by one, and select all stars in the valid range. # Then add them to the catalog with RAs and DECs full_catalog = None # numpy.zeros(shape=(0,6)) catalog_filenames = [] # Start iterating though catalogs for catalogname in files_to_read: catalogfile = "%s/%s" % (self.basedir, catalogname) # print catalogfile if (not os.path.isfile(catalogfile)): # not a file, try adding .fits to the end of the filename if (os.path.isfile(catalogfile + ".fits")): catalogfile += ".fits" else: # neither option (w/ or w/o .fits added is a file) _logger.warning( "Catalog file (%s) not found (base-dir: %s)" % (os.path.abspath(catalogfile), self.basedir)) continue try: hdu_cat = fits.open(catalogfile) except: _logger.warning("Unable to open catalog file %s" % (catalogfile)) continue catalog_filenames.append(catalogfile) _logger.debug("Adding %s to list of catalog files being used" % (catalogfile)) # read table into a nd-array buffer cat_full = hdu_cat[1].data hdu_cat.close() # Read the RA and DEC values cat_ra = cat_full['RA'] cat_dec = cat_full['DEC'] # To select the right region, shift a temporary catalog cat_ra_shifted = cat_ra if (max_ra > 360.): cat_ra_shifted[cat_ra < 180] += 360 elif (min_ra < 0): cat_ra_shifted[cat_ra > 180] -= 360 select_from_cat = (cat_ra_shifted > min_ra) & (cat_ra_shifted < max_ra) & (cat_dec > min_dec) & ( cat_dec < max_dec) array_to_add = cat_full[select_from_cat] _logger.debug("Read %d sources from %s" % (array_to_add.shape[0], catalogname)) if (full_catalog is None): full_catalog = array_to_add else: full_catalog = np.append(full_catalog, array_to_add, axis=0) # print photom_grizy[:3,:] if (full_catalog is None): _logger.warning("No stars found in area %s, %s from catalog %s" % ( str(ra), str(dec), # ra[0], ra[1], dec[0], dec[1], self.basedir)) else: _logger.debug( "Read a total of %d stars from %d catalogs!" % (full_catalog.shape[0], len(files_to_read))) self.PS1toSDSS(full_catalog) return full_catalog #### Wrapper routines to use photometric zeropointing stand-alone def crawlDirectory(directory, db, args): search = "%s/*-[es][19]1.fits.fz" % (directory) inputlist = glob.glob(search) initialsize = len (inputlist) rejects = [] if not args.redo: for image in inputlist: if db.exists(image): rejects.append (image) for r in rejects: inputlist.remove (r) print ("Found %d files intially, but cleaned %d already measured images. Starting analysis of %d files" % (initialsize, len(rejects), len(inputlist))) photzpStage = PhotCalib(args.ps1dir) for image in inputlist: image = image.rstrip() photzpStage.analyzeImage(image, outputdb=db, outputimageRootDir=args.outputimageRootDir, mintexp=args.mintexp) def crawlSiteCameraArchive(site, camera, args, date=None): ''' Process in the archive
:param site: :param camera:
random_line_split
photcalibration.py
only from the input catalog. ## Why reverse the order of the color term entries? Data are entered in the order as they are ## shown in paper. Reverse after the fact to avoid confusion when looking at paper ps1colorterms = {} ps1colorterms['g'] = [-0.01808, -0.13595, +0.01941, -0.00183][::-1] ps1colorterms['r'] = [-0.01836, -0.03577, +0.02612, -0.00558][::-1] ps1colorterms['i'] = [+0.01170, -0.00400, +0.00066, -0.00058][::-1] ps1colorterms['z'] = [-0.01062, +0.07529, -0.03592, +0.00890][::-1] def __init__(self, basedir): self.basedir = basedir self.skytable = None def PS1toSDSS(self, table): """ Modify table in situ from PS1 to SDSS, requires column names compatible with ps1colorterms definition. :param table: :return: modified table. """ if table is not None: pscolor = table['g'] - table['i'] for filter in self.ps1colorterms: colorcorrection = np.polyval(self.ps1colorterms[filter], pscolor) table[filter] -= colorcorrection return table def isInCatalogFootprint(self, ra, dec): """ Verify if image is in catalog footprint. TODO: Account for image field of view """ # PanSTARRS has valid entries for DEc > - 30 degrees return dec >= -30.0 def get_reference_catalog(self, ra, dec, radius, overwrite_select=False): """ Read i fits table from local catalog copy. Concatenate tables columns from different fits tables for full coverage. """ # A lot of safeguarding boiler plate to ensure catalog files are valid. if (self.basedir is None) or (not os.path.isdir(self.basedir)): _logger.error("Unable to find reference catalog: %s" % (str(self.basedir))) return None # Load the SkyTable so we know in what files to look for the catalog" _logger.debug("Using catalog found in %s" % (self.basedir)) skytable_filename = "%s/SkyTable.fits" % (self.basedir) if (not os.path.isfile(skytable_filename)): _logger.fatal("Unable to find catalog index file in %s!" % (self.basedir)) return None # Read in the master index hdu skytable_hdu = fits.open(skytable_filename) skytable = skytable_hdu['SKY_REGION'].data # Select entries that match our list # print ra, dec, radius, type(ra), type(dec), type(radius) # logger.debug("# Searching for stars within %.1f degress around %f , %f ..." % (radius, ra, dec)) if (not radius == None and radius > 0): min_dec = dec - radius max_dec = dec + radius min_ra = ra - radius / math.cos(math.radians(dec)) max_ra = ra + radius / math.cos(math.radians(dec)) else: min_dec, max_dec = dec[0], dec[1] min_ra, max_ra = ra[0], ra[1] _logger.debug("Querying catalog: Ra=%f...%f Dec=%f...%f" % (min_ra, max_ra, min_dec, max_dec)) if (max_ra > 360.): # This wraps around the high end, shift all ra values by -180 # Now all search RAs are ok and around the 180, next also move the catalog values selected = skytable['R_MIN'] < 180 skytable['R_MAX'][selected] += 360 skytable['R_MIN'][selected] += 360 if (min_ra < 0): # Wrap around at the low end selected = skytable['R_MAX'] > 180 skytable['R_MAX'][selected] -= 360 skytable['R_MIN'][selected] -= 360 _logger.debug("# Search radius: RA=%.1f ... %.1f DEC=%.1f ... %.1f" % (min_ra, max_ra, min_dec, max_dec)) try: needed_catalogs = (skytable['PARENT'] > 0) & (skytable['PARENT'] < 25) & \ (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) except KeyError: # try without the PARENT field needed_catalogs = (skytable['R_MAX'] > min_ra) & (skytable['R_MIN'] < max_ra) & \ (skytable['D_MAX'] > min_dec) & (skytable['D_MIN'] < max_dec) # print skytable[needed_catalogs] files_to_read = skytable['NAME'][needed_catalogs] files_to_read = [f.strip() for f in files_to_read] _logger.debug(files_to_read) skytable_hdu.close() # Warning: might erase the loaded data, might need to copy array! # Now quickly go over the list and take care of all filenames that still have a 0x00 in them for i in range(len(files_to_read)): found_at = files_to_read[i].find('\0') if (found_at > 0): files_to_read[i] = files_to_read[i][:found_at] # Load all frames, one by one, and select all stars in the valid range. # Then add them to the catalog with RAs and DECs full_catalog = None # numpy.zeros(shape=(0,6)) catalog_filenames = [] # Start iterating though catalogs for catalogname in files_to_read: catalogfile = "%s/%s" % (self.basedir, catalogname) # print catalogfile if (not os.path.isfile(catalogfile)): # not a file, try adding .fits to the end of the filename if (os.path.isfile(catalogfile + ".fits")): catalogfile += ".fits" else: # neither option (w/ or w/o .fits added is a file) _logger.warning( "Catalog file (%s) not found (base-dir: %s)" % (os.path.abspath(catalogfile), self.basedir)) continue try: hdu_cat = fits.open(catalogfile) except: _logger.warning("Unable to open catalog file %s" % (catalogfile)) continue catalog_filenames.append(catalogfile) _logger.debug("Adding %s to list of catalog files being used" % (catalogfile)) # read table into a nd-array buffer cat_full = hdu_cat[1].data hdu_cat.close() # Read the RA and DEC values cat_ra = cat_full['RA'] cat_dec = cat_full['DEC'] # To select the right region, shift a temporary catalog cat_ra_shifted = cat_ra if (max_ra > 360.): cat_ra_shifted[cat_ra < 180] += 360 elif (min_ra < 0): cat_ra_shifted[cat_ra > 180] -= 360 select_from_cat = (cat_ra_shifted > min_ra) & (cat_ra_shifted < max_ra) & (cat_dec > min_dec) & ( cat_dec < max_dec) array_to_add = cat_full[select_from_cat] _logger.debug("Read %d sources from %s" % (array_to_add.shape[0], catalogname)) if (full_catalog is None): full_catalog = array_to_add else: full_catalog = np.append(full_catalog, array_to_add, axis=0) # print photom_grizy[:3,:] if (full_catalog is None): _logger.warning("No stars found in area %s, %s from catalog %s" % ( str(ra), str(dec), # ra[0], ra[1], dec[0], dec[1], self.basedir)) else: _logger.debug( "Read a total of %d stars from %d catalogs!" % (full_catalog.shape[0], len(files_to_read))) self.PS1toSDSS(full_catalog) return full_catalog #### Wrapper routines to use photometric zeropointing stand-alone def
crawlDirectory
identifier_name
genetic.py
states if DEBUG: print(gene_pool) counter = 0
flips = 0 while counter < GIVE_UP: if rand_restarts and counter>0 and not (counter % restart): # random restarts if not tb: print("restarting: (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) + ")") initialize_states(gene_pool, gene_pool[0]) if not tb: print("iteration", counter, "\t\t", gene_pool[0]) new_pool = [] evaluations = eval_pool(gene_pool) elite_states = add_elite(gene_pool, new_pool, evaluations) select(gene_pool, new_pool, evaluations) crossover(2, new_pool) mutate(2, new_pool) new_vals = eval_pool(new_pool) flips += flip_heuristic2(2, new_pool, new_vals) #record(gene_pool) # records chnges in pool to history if False and DEBUG: print(counter, "old:",gene_pool, evaluations) print(counter, "new:",new_pool, new_vals) gene_pool = new_pool if evaluate(gene_pool[0]) == NUM_CLAUSES: if not tb: # test bench print(">> PROBLEM SATISFIED at iteration " + str(counter)) print(">> With solution:", readable(gene_pool[0])) print(">> Satisfied (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) +") clauses.") return (0, flips) counter += 1 if not tb: # test bench print(">> GAVE UP after " + str(GIVE_UP) + " tries.") print(">> Current Best:", readable(gene_pool[0])) print(">> Satisfied (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) +") clauses.") print("UNSATISFIED CLAUSES: (1-indexed)") for i in range(len(CNF)): if not satisfied(CNF[i], gene_pool[0]): print(str(i+1) + ":\t", CNF[i]) return (1, flips) def readable(string): new_str = "" for i in range(len(string)): if i%5 == 0: new_str += " " new_str += string[i] return new_str def flip_coin(p=.5): if (p < 0 or p > 1): raise ValueError("p can only be between 0 and 1.") return int(random.random() < p) def sample(probs, selections): samples = [] cdf = [] for i in range(len(probs)-1): cdf.append(sum(probs[:i+1])) cdf.append(1) while len(samples) < selections: p = random.random() i = 0 while(p >= cdf[i]): i += 1 samples.append(i) return samples def print_pool(gene_pool): for i in range(len(gene_pool)): print(gene_pool[i]) def record(gene_pool): history.append(copy.deepcopy(gene_pool)) def initialize_states(gene_pool, given=None): if given and (len(given)<len(gene_pool)): for i in range(len(given)): gene_pool[i] = given[i] for i in range(len(given), POOL_SIZE): gene_pool[i] = create_state() else: for i in range(POOL_SIZE): gene_pool[i] = create_state() def create_state(): state = "" for i in range(NUM_VARS): state += str(int(flip_coin())) return state #TODO: to be replaced with cnf evaluation function for sat problems ''' def evaluate(state): #result = 0 #for i in range(NUM_CLAUSES): # if state[i] == goal[i]: # result += 1 #return result return cnf_eval(CNF, state) ''' def eval_pool(pool): evaluations = [] for i in range(len(pool)): evaluations.append(evaluate(pool[i])) return evaluations def add_elite(old_pool, new_pool, evaluations): best1 = old_pool[0] b1_score = evaluations[0] best2 = best1 # keep the same for now b2_score = b1_score for i in range(1, POOL_SIZE): if evaluations[i] >= b1_score: # shuffles around ties best2 = best1 b2_score = b1_score best1 = old_pool[i] b1_score = evaluations[i] new_pool.append(best1) new_pool.append(best2) def select(gene_pool, new_pool, evaluations): probs = [0 for i in range(POOL_SIZE)] selections = POOL_SIZE - len(new_pool) denom = sum(evaluations) for i in range(POOL_SIZE): probs[i] = evaluations[i]/denom result = sample(probs, selections) for i in range(selections): new_pool.append(gene_pool[result[i]]) def crossover(safe, new_pool): for i in range(safe, POOL_SIZE, 2): a, b = cross(new_pool[i], new_pool[i+1]) new_pool[i] = a new_pool[i+1] = b def cross(x, y): guide = "" for i in range(len(x)): guide += str(int(flip_coin())) c1 = "" c2 = "" for i in range(len(x)): if guide[i] == "1": c1 += x[i] c2 += y[i] else: c1 += y[i] c2 += x[i] return (c1, c2) def mutate(safe, new_pool): for i in range(safe, POOL_SIZE): if flip_coin(.9): mutant = "" for j in range(len(new_pool[i])): if flip_coin(): mutant += str(1 - int(new_pool[i][j])) else: mutant += new_pool[i][j] new_pool[i] = mutant # the actual flip heuristic (local changes) # now, should instantly solve convex optimizations def flip_heuristic2(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): improvement = True order = [j for j in range(NUM_VARS)] flips = 0 while improvement: # keeps going as long as there is improvement improvement = False random.shuffle(order) for j in order: new_str, new_eval = eval_flip(new_pool[i], j, evaluations[i]) flips +=1 if new_str: # eval flip returns None if not better new_pool[i] = new_str evaluations[i] = new_eval #improvement = True return flips def eval_flip(string, index, evaluation): # flipping bit at index i new_str = string[:index] + ("1" if string[index]=="0" else "0") + string[(index+1):] new_eval = evaluate(new_str) if new_eval > evaluation: return (new_str, new_eval) return (None, None) # not currently using def flip_heuristic(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): flipped = flip_bits(new_pool[i]) value = evaluate(flipped) if value >= evaluations[i]: evaluations[i] = value new_pool[i] = flipped def flip_bits(string): new_str = "" for i in range(NUM_VARS): new_str += "1" if string[i]=="0" else "0" return new_str # right now, assuming that lines end with zero, one clause per line def read_cnf(file_name): f = open(file_name, "r") lines = f.read().splitlines() variables = 0 clauses = 0 for i in range(len(lines)): if lines[i][0] == 'p': # found problem line variables, clauses = map(int, lines[i].split()[2:]) if DEBUG: print(variables, clauses) lines = lines[(i+1):] break; cnf = [] for i in range(clauses): cnf.append(list(map(int, lines[i].split()))) for clause in cnf: # removes 0 from the end (assumption) if clause[-1] == 0: clause.pop() return (variables, cnf) def cnf_eval(cnf, state): sat_clauses = 0 for i in range(len(cnf)): sat = satisfied(cnf[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat) if sat: sat_clauses += 1 return sat_clauses def evaluate(state): sat_clauses = 0 for i in range(NUM_CLAUSES): sat = satisfied(CNF[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat) if sat
restart = int(tries/5)
random_line_split
genetic.py
states if DEBUG: print(gene_pool) counter = 0 restart = int(tries/5) flips = 0 while counter < GIVE_UP: if rand_restarts and counter>0 and not (counter % restart): # random restarts if not tb: print("restarting: (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) + ")") initialize_states(gene_pool, gene_pool[0]) if not tb: print("iteration", counter, "\t\t", gene_pool[0]) new_pool = [] evaluations = eval_pool(gene_pool) elite_states = add_elite(gene_pool, new_pool, evaluations) select(gene_pool, new_pool, evaluations) crossover(2, new_pool) mutate(2, new_pool) new_vals = eval_pool(new_pool) flips += flip_heuristic2(2, new_pool, new_vals) #record(gene_pool) # records chnges in pool to history if False and DEBUG: print(counter, "old:",gene_pool, evaluations) print(counter, "new:",new_pool, new_vals) gene_pool = new_pool if evaluate(gene_pool[0]) == NUM_CLAUSES: if not tb: # test bench print(">> PROBLEM SATISFIED at iteration " + str(counter)) print(">> With solution:", readable(gene_pool[0])) print(">> Satisfied (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) +") clauses.") return (0, flips) counter += 1 if not tb: # test bench print(">> GAVE UP after " + str(GIVE_UP) + " tries.") print(">> Current Best:", readable(gene_pool[0])) print(">> Satisfied (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) +") clauses.") print("UNSATISFIED CLAUSES: (1-indexed)") for i in range(len(CNF)): if not satisfied(CNF[i], gene_pool[0]): print(str(i+1) + ":\t", CNF[i]) return (1, flips) def readable(string): new_str = "" for i in range(len(string)): if i%5 == 0: new_str += " " new_str += string[i] return new_str def flip_coin(p=.5): if (p < 0 or p > 1): raise ValueError("p can only be between 0 and 1.") return int(random.random() < p) def sample(probs, selections): samples = [] cdf = [] for i in range(len(probs)-1): cdf.append(sum(probs[:i+1])) cdf.append(1) while len(samples) < selections: p = random.random() i = 0 while(p >= cdf[i]): i += 1 samples.append(i) return samples def print_pool(gene_pool): for i in range(len(gene_pool)): print(gene_pool[i]) def record(gene_pool): history.append(copy.deepcopy(gene_pool)) def initialize_states(gene_pool, given=None): if given and (len(given)<len(gene_pool)): for i in range(len(given)): gene_pool[i] = given[i] for i in range(len(given), POOL_SIZE): gene_pool[i] = create_state() else: for i in range(POOL_SIZE): gene_pool[i] = create_state() def create_state(): state = "" for i in range(NUM_VARS): state += str(int(flip_coin())) return state #TODO: to be replaced with cnf evaluation function for sat problems ''' def evaluate(state): #result = 0 #for i in range(NUM_CLAUSES): # if state[i] == goal[i]: # result += 1 #return result return cnf_eval(CNF, state) ''' def eval_pool(pool): evaluations = [] for i in range(len(pool)): evaluations.append(evaluate(pool[i])) return evaluations def add_elite(old_pool, new_pool, evaluations): best1 = old_pool[0] b1_score = evaluations[0] best2 = best1 # keep the same for now b2_score = b1_score for i in range(1, POOL_SIZE): if evaluations[i] >= b1_score: # shuffles around ties best2 = best1 b2_score = b1_score best1 = old_pool[i] b1_score = evaluations[i] new_pool.append(best1) new_pool.append(best2) def select(gene_pool, new_pool, evaluations): probs = [0 for i in range(POOL_SIZE)] selections = POOL_SIZE - len(new_pool) denom = sum(evaluations) for i in range(POOL_SIZE): probs[i] = evaluations[i]/denom result = sample(probs, selections) for i in range(selections): new_pool.append(gene_pool[result[i]]) def crossover(safe, new_pool): for i in range(safe, POOL_SIZE, 2): a, b = cross(new_pool[i], new_pool[i+1]) new_pool[i] = a new_pool[i+1] = b def cross(x, y): guide = "" for i in range(len(x)): guide += str(int(flip_coin())) c1 = "" c2 = "" for i in range(len(x)): if guide[i] == "1": c1 += x[i] c2 += y[i] else: c1 += y[i] c2 += x[i] return (c1, c2) def mutate(safe, new_pool): for i in range(safe, POOL_SIZE): if flip_coin(.9): mutant = "" for j in range(len(new_pool[i])): if flip_coin(): mutant += str(1 - int(new_pool[i][j])) else: mutant += new_pool[i][j] new_pool[i] = mutant # the actual flip heuristic (local changes) # now, should instantly solve convex optimizations def flip_heuristic2(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): improvement = True order = [j for j in range(NUM_VARS)] flips = 0 while improvement: # keeps going as long as there is improvement improvement = False random.shuffle(order) for j in order: new_str, new_eval = eval_flip(new_pool[i], j, evaluations[i]) flips +=1 if new_str: # eval flip returns None if not better new_pool[i] = new_str evaluations[i] = new_eval #improvement = True return flips def eval_flip(string, index, evaluation): # flipping bit at index i
# not currently using def flip_heuristic(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): flipped = flip_bits(new_pool[i]) value = evaluate(flipped) if value >= evaluations[i]: evaluations[i] = value new_pool[i] = flipped def flip_bits(string): new_str = "" for i in range(NUM_VARS): new_str += "1" if string[i]=="0" else "0" return new_str # right now, assuming that lines end with zero, one clause per line def read_cnf(file_name): f = open(file_name, "r") lines = f.read().splitlines() variables = 0 clauses = 0 for i in range(len(lines)): if lines[i][0] == 'p': # found problem line variables, clauses = map(int, lines[i].split()[2:]) if DEBUG: print(variables, clauses) lines = lines[(i+1):] break; cnf = [] for i in range(clauses): cnf.append(list(map(int, lines[i].split()))) for clause in cnf: # removes 0 from the end (assumption) if clause[-1] == 0: clause.pop() return (variables, cnf) def cnf_eval(cnf, state): sat_clauses = 0 for i in range(len(cnf)): sat = satisfied(cnf[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat) if sat: sat_clauses += 1 return sat_clauses def evaluate(state): sat_clauses = 0 for i in range(NUM_CLAUSES): sat = satisfied(CNF[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat)
new_str = string[:index] + ("1" if string[index]=="0" else "0") + string[(index+1):] new_eval = evaluate(new_str) if new_eval > evaluation: return (new_str, new_eval) return (None, None)
identifier_body
genetic.py
(NUM_CLAUSES) +") clauses.") return (0, flips) counter += 1 if not tb: # test bench print(">> GAVE UP after " + str(GIVE_UP) + " tries.") print(">> Current Best:", readable(gene_pool[0])) print(">> Satisfied (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) +") clauses.") print("UNSATISFIED CLAUSES: (1-indexed)") for i in range(len(CNF)): if not satisfied(CNF[i], gene_pool[0]): print(str(i+1) + ":\t", CNF[i]) return (1, flips) def readable(string): new_str = "" for i in range(len(string)): if i%5 == 0: new_str += " " new_str += string[i] return new_str def flip_coin(p=.5): if (p < 0 or p > 1): raise ValueError("p can only be between 0 and 1.") return int(random.random() < p) def sample(probs, selections): samples = [] cdf = [] for i in range(len(probs)-1): cdf.append(sum(probs[:i+1])) cdf.append(1) while len(samples) < selections: p = random.random() i = 0 while(p >= cdf[i]): i += 1 samples.append(i) return samples def print_pool(gene_pool): for i in range(len(gene_pool)): print(gene_pool[i]) def record(gene_pool): history.append(copy.deepcopy(gene_pool)) def initialize_states(gene_pool, given=None): if given and (len(given)<len(gene_pool)): for i in range(len(given)): gene_pool[i] = given[i] for i in range(len(given), POOL_SIZE): gene_pool[i] = create_state() else: for i in range(POOL_SIZE): gene_pool[i] = create_state() def create_state(): state = "" for i in range(NUM_VARS): state += str(int(flip_coin())) return state #TODO: to be replaced with cnf evaluation function for sat problems ''' def evaluate(state): #result = 0 #for i in range(NUM_CLAUSES): # if state[i] == goal[i]: # result += 1 #return result return cnf_eval(CNF, state) ''' def eval_pool(pool): evaluations = [] for i in range(len(pool)): evaluations.append(evaluate(pool[i])) return evaluations def add_elite(old_pool, new_pool, evaluations): best1 = old_pool[0] b1_score = evaluations[0] best2 = best1 # keep the same for now b2_score = b1_score for i in range(1, POOL_SIZE): if evaluations[i] >= b1_score: # shuffles around ties best2 = best1 b2_score = b1_score best1 = old_pool[i] b1_score = evaluations[i] new_pool.append(best1) new_pool.append(best2) def select(gene_pool, new_pool, evaluations): probs = [0 for i in range(POOL_SIZE)] selections = POOL_SIZE - len(new_pool) denom = sum(evaluations) for i in range(POOL_SIZE): probs[i] = evaluations[i]/denom result = sample(probs, selections) for i in range(selections): new_pool.append(gene_pool[result[i]]) def crossover(safe, new_pool): for i in range(safe, POOL_SIZE, 2): a, b = cross(new_pool[i], new_pool[i+1]) new_pool[i] = a new_pool[i+1] = b def cross(x, y): guide = "" for i in range(len(x)): guide += str(int(flip_coin())) c1 = "" c2 = "" for i in range(len(x)): if guide[i] == "1": c1 += x[i] c2 += y[i] else: c1 += y[i] c2 += x[i] return (c1, c2) def mutate(safe, new_pool): for i in range(safe, POOL_SIZE): if flip_coin(.9): mutant = "" for j in range(len(new_pool[i])): if flip_coin(): mutant += str(1 - int(new_pool[i][j])) else: mutant += new_pool[i][j] new_pool[i] = mutant # the actual flip heuristic (local changes) # now, should instantly solve convex optimizations def flip_heuristic2(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): improvement = True order = [j for j in range(NUM_VARS)] flips = 0 while improvement: # keeps going as long as there is improvement improvement = False random.shuffle(order) for j in order: new_str, new_eval = eval_flip(new_pool[i], j, evaluations[i]) flips +=1 if new_str: # eval flip returns None if not better new_pool[i] = new_str evaluations[i] = new_eval #improvement = True return flips def eval_flip(string, index, evaluation): # flipping bit at index i new_str = string[:index] + ("1" if string[index]=="0" else "0") + string[(index+1):] new_eval = evaluate(new_str) if new_eval > evaluation: return (new_str, new_eval) return (None, None) # not currently using def flip_heuristic(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): flipped = flip_bits(new_pool[i]) value = evaluate(flipped) if value >= evaluations[i]: evaluations[i] = value new_pool[i] = flipped def flip_bits(string): new_str = "" for i in range(NUM_VARS): new_str += "1" if string[i]=="0" else "0" return new_str # right now, assuming that lines end with zero, one clause per line def read_cnf(file_name): f = open(file_name, "r") lines = f.read().splitlines() variables = 0 clauses = 0 for i in range(len(lines)): if lines[i][0] == 'p': # found problem line variables, clauses = map(int, lines[i].split()[2:]) if DEBUG: print(variables, clauses) lines = lines[(i+1):] break; cnf = [] for i in range(clauses): cnf.append(list(map(int, lines[i].split()))) for clause in cnf: # removes 0 from the end (assumption) if clause[-1] == 0: clause.pop() return (variables, cnf) def cnf_eval(cnf, state): sat_clauses = 0 for i in range(len(cnf)): sat = satisfied(cnf[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat) if sat: sat_clauses += 1 return sat_clauses def evaluate(state): sat_clauses = 0 for i in range(NUM_CLAUSES): sat = satisfied(CNF[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat) if sat: sat_clauses += 1 return sat_clauses # simple, doesn't tell you how satisfied the clause is (don't think that matters) def satisfied(clause, state): #for i in range(len(clause)): for i in range(3): temp = -clause[i] if (clause[i] < 0) else clause[i] # (if the variable is true) != (if the variable is negated) truthy = (state[temp-1]=="1") != (temp==clause[i]) if truthy: return True return False ###### def generate_tsp(cities, rand_state=None): if rand_state != None: random.seed(rand_state) l = [] # list c_list = set() rand = random.randrange # localizing function for i in range(cities): while True: x = rand(100) y = rand(100) if (x,y) not in c_list: break l.append((i, (x, y))) c_list.add((x,y)) return l def init_adj(cities): # create adjacency matrix num = len(cities) inf = float('inf') res = [[0 for i in range(num)] for j in range(num)] for i in range(num):
for j in range(num): res[i][j] = calc_dist(cities[i][1], cities[j][1]) res[j][i] = res[i][j]
conditional_block
genetic.py
if DEBUG: print(gene_pool) counter = 0 restart = int(tries/5) flips = 0 while counter < GIVE_UP: if rand_restarts and counter>0 and not (counter % restart): # random restarts if not tb: print("restarting: (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) + ")") initialize_states(gene_pool, gene_pool[0]) if not tb: print("iteration", counter, "\t\t", gene_pool[0]) new_pool = [] evaluations = eval_pool(gene_pool) elite_states = add_elite(gene_pool, new_pool, evaluations) select(gene_pool, new_pool, evaluations) crossover(2, new_pool) mutate(2, new_pool) new_vals = eval_pool(new_pool) flips += flip_heuristic2(2, new_pool, new_vals) #record(gene_pool) # records chnges in pool to history if False and DEBUG: print(counter, "old:",gene_pool, evaluations) print(counter, "new:",new_pool, new_vals) gene_pool = new_pool if evaluate(gene_pool[0]) == NUM_CLAUSES: if not tb: # test bench print(">> PROBLEM SATISFIED at iteration " + str(counter)) print(">> With solution:", readable(gene_pool[0])) print(">> Satisfied (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) +") clauses.") return (0, flips) counter += 1 if not tb: # test bench print(">> GAVE UP after " + str(GIVE_UP) + " tries.") print(">> Current Best:", readable(gene_pool[0])) print(">> Satisfied (" + str(new_vals[0]) + "/" + str(NUM_CLAUSES) +") clauses.") print("UNSATISFIED CLAUSES: (1-indexed)") for i in range(len(CNF)): if not satisfied(CNF[i], gene_pool[0]): print(str(i+1) + ":\t", CNF[i]) return (1, flips) def readable(string): new_str = "" for i in range(len(string)): if i%5 == 0: new_str += " " new_str += string[i] return new_str def flip_coin(p=.5): if (p < 0 or p > 1): raise ValueError("p can only be between 0 and 1.") return int(random.random() < p) def sample(probs, selections): samples = [] cdf = [] for i in range(len(probs)-1): cdf.append(sum(probs[:i+1])) cdf.append(1) while len(samples) < selections: p = random.random() i = 0 while(p >= cdf[i]): i += 1 samples.append(i) return samples def print_pool(gene_pool): for i in range(len(gene_pool)): print(gene_pool[i]) def record(gene_pool): history.append(copy.deepcopy(gene_pool)) def initialize_states(gene_pool, given=None): if given and (len(given)<len(gene_pool)): for i in range(len(given)): gene_pool[i] = given[i] for i in range(len(given), POOL_SIZE): gene_pool[i] = create_state() else: for i in range(POOL_SIZE): gene_pool[i] = create_state() def create_state(): state = "" for i in range(NUM_VARS): state += str(int(flip_coin())) return state #TODO: to be replaced with cnf evaluation function for sat problems ''' def evaluate(state): #result = 0 #for i in range(NUM_CLAUSES): # if state[i] == goal[i]: # result += 1 #return result return cnf_eval(CNF, state) ''' def eval_pool(pool): evaluations = [] for i in range(len(pool)): evaluations.append(evaluate(pool[i])) return evaluations def add_elite(old_pool, new_pool, evaluations): best1 = old_pool[0] b1_score = evaluations[0] best2 = best1 # keep the same for now b2_score = b1_score for i in range(1, POOL_SIZE): if evaluations[i] >= b1_score: # shuffles around ties best2 = best1 b2_score = b1_score best1 = old_pool[i] b1_score = evaluations[i] new_pool.append(best1) new_pool.append(best2) def select(gene_pool, new_pool, evaluations): probs = [0 for i in range(POOL_SIZE)] selections = POOL_SIZE - len(new_pool) denom = sum(evaluations) for i in range(POOL_SIZE): probs[i] = evaluations[i]/denom result = sample(probs, selections) for i in range(selections): new_pool.append(gene_pool[result[i]]) def crossover(safe, new_pool): for i in range(safe, POOL_SIZE, 2): a, b = cross(new_pool[i], new_pool[i+1]) new_pool[i] = a new_pool[i+1] = b def cross(x, y): guide = "" for i in range(len(x)): guide += str(int(flip_coin())) c1 = "" c2 = "" for i in range(len(x)): if guide[i] == "1": c1 += x[i] c2 += y[i] else: c1 += y[i] c2 += x[i] return (c1, c2) def
(safe, new_pool): for i in range(safe, POOL_SIZE): if flip_coin(.9): mutant = "" for j in range(len(new_pool[i])): if flip_coin(): mutant += str(1 - int(new_pool[i][j])) else: mutant += new_pool[i][j] new_pool[i] = mutant # the actual flip heuristic (local changes) # now, should instantly solve convex optimizations def flip_heuristic2(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): improvement = True order = [j for j in range(NUM_VARS)] flips = 0 while improvement: # keeps going as long as there is improvement improvement = False random.shuffle(order) for j in order: new_str, new_eval = eval_flip(new_pool[i], j, evaluations[i]) flips +=1 if new_str: # eval flip returns None if not better new_pool[i] = new_str evaluations[i] = new_eval #improvement = True return flips def eval_flip(string, index, evaluation): # flipping bit at index i new_str = string[:index] + ("1" if string[index]=="0" else "0") + string[(index+1):] new_eval = evaluate(new_str) if new_eval > evaluation: return (new_str, new_eval) return (None, None) # not currently using def flip_heuristic(safe, new_pool, evaluations): for i in range(safe, POOL_SIZE): flipped = flip_bits(new_pool[i]) value = evaluate(flipped) if value >= evaluations[i]: evaluations[i] = value new_pool[i] = flipped def flip_bits(string): new_str = "" for i in range(NUM_VARS): new_str += "1" if string[i]=="0" else "0" return new_str # right now, assuming that lines end with zero, one clause per line def read_cnf(file_name): f = open(file_name, "r") lines = f.read().splitlines() variables = 0 clauses = 0 for i in range(len(lines)): if lines[i][0] == 'p': # found problem line variables, clauses = map(int, lines[i].split()[2:]) if DEBUG: print(variables, clauses) lines = lines[(i+1):] break; cnf = [] for i in range(clauses): cnf.append(list(map(int, lines[i].split()))) for clause in cnf: # removes 0 from the end (assumption) if clause[-1] == 0: clause.pop() return (variables, cnf) def cnf_eval(cnf, state): sat_clauses = 0 for i in range(len(cnf)): sat = satisfied(cnf[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat) if sat: sat_clauses += 1 return sat_clauses def evaluate(state): sat_clauses = 0 for i in range(NUM_CLAUSES): sat = satisfied(CNF[i], state) #if DEBUG: print(i, sat_clauses, "c:",cnf[i],"s:",state, sat)
mutate
identifier_name
Data_Exploration.py
l.append(d) # theresult_json = json.dumps(l, default=json_serial) conn.close() return theresult_json @app.route("/api/correlation/<col1>/<col2>") def Correlation(col1, col2): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select corr(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/covariance/<col1>/<col2>", methods=['GET']) def Covariance(col1, col2): """Determine the covariance coefficient between two columns.""" engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select covar_samp(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/histogram/<groupby>/<count>") def Histogram(groupby, count): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT %s AS Group, count(%s) AS Count FROM orders Group by %s Order by count(%s) DESC """ % (groupby, count, groupby, count) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'Group': result[0], 'Count': result[1]} l.append(d) theresult_json = json.dumps(l) conn.close() return theresult_json @app.route('/api/add_message/<uuid>', methods=['GET', 'POST']) def add_message(uuid): content = request.get_json(silent=True) print (content) return jsonify('{"h" : "ok"}') @app.route('/api/asterixwrap', methods=['GET']) def api_asterixwrap(): sql = """USE AstxDB; select * from TableBT v; """ ads = AsterixDataSource() jsonobj = ads.execute(sql) return jsonify(jsonobj) def getNodeIds(category_list): _where = ' OR '.join(['user.category.nested.nested.level_2 = "{0}"'.format(x) for x in category_list]) sql=""" use bookstore_dp; select user.nodeID from ClassificationInfo user WHERE {0} """.format(_where) # where user.category.nested.nested.level_2 = "Education & Reference"; ads = AsterixDataSource(host="132.249.238.32") jsonobj = ads.execute(sql) return jsonobj @app.route("/api/highest_monthly_sales_by_category/<list>") def api_highest_monthly_sales_by_category(list): category_list = list.split(",") # print(category_list) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') #engine = create_engine('postgresql+psycopg2://postgres@45.79.91.219/MyBookStore') # conn = engine.connect() # _jlist = getNodeIds(category_list) # will be replaced by asterix call once connected to DB - the result will not change though print(_jlist) _inStr = convertToIn(_jlist) # print(_inStr) # sql = """ SELECT mon, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.productid, EXTRACT(MONTH from billdate) order by p.productid ) monthlysales WHERE category IN {0} GROUP By mon ORDER BY num_sold DESC """.format(_inStr) # print(sql) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'mon': int(result[0]), 'num_sold' : int(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() return (theresult_json) @app.route('/api/solrwrap', methods=['GET']) def api_solrwrap(): q = "*:*" ads = SolrDataSource() jsonobj = ads.execute(q) return jsonify(jsonobj) @app.route("/api/Top_Categories/<num_categories>/<months>") def Top_categories(num_categories, months): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid, EXTRACT(MONTH from billdate) order by p.nodeid ) monthlysales WHERE mon in ({0}) GROUP BY category ORDER BY num_sold DESC LIMIT ({1}) """.format(months,num_categories) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'category': result[0], 'num_sold': float(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() # return (results) # print(theresult_json) return (theresult_json) @app.route("/api/Discontinue_Stocking/<threshold>/<startyear>/<endyear>") def Discontinue_Stocking(threshold, startyear, endyear): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category FROM ( select EXTRACT(YEAR from o.billdate) as yr, p.nodeid as category, sum(o.numunits) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid , EXTRACT(YEAR from o.billdate) order by p.nodeid ) yearly_sales where books_sold < {0} AND (yr < {2} AND yr >= {1}) """.format(threshold,startyear, endyear) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'category': result[0]} l.append(d) theresult_json = json.dumps(l) conn.close() # return (results) # print(theresult_json) return (theresult_json) @app.route("/api/Downward_Sales/<season>") def Downware_Sales(season): # return jsonify({ "n" : num_categories, "m" : months}) seasons = {'spring':(3,4,5), 'summer':(6,7,8), 'fall':(9,10,11), 'winter':(12,1,2) } seasontrend = seasons.get((season.lower())) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT s.category, round(avg(s.change_in_sales_from_last_month)) AS sale_trend FROM ( SELECT category, mon, count(mon) OVER (PART
c = request.args[item] print (c) d[c] = result[c]
conditional_block
Data_Exploration.py
@app.route("/api/web_method/<format>") def api_web_method(format): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select * from orderlines o, products p where o.productid = p.productid LIMIT 10 """ stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {} for item in request.args: c = request.args[item] print (c) d[c] = result[c] l.append(d) # theresult_json = json.dumps(l, default=json_serial) conn.close() return theresult_json @app.route("/api/correlation/<col1>/<col2>") def Correlation(col1, col2): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select corr(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/covariance/<col1>/<col2>", methods=['GET']) def Covariance(col1, col2): """Determine the covariance coefficient between two columns.""" engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select covar_samp(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/histogram/<groupby>/<count>") def Histogram(groupby, count): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT %s AS Group, count(%s) AS Count FROM orders Group by %s Order by count(%s) DESC """ % (groupby, count, groupby, count) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'Group': result[0], 'Count': result[1]} l.append(d) theresult_json = json.dumps(l) conn.close() return theresult_json @app.route('/api/add_message/<uuid>', methods=['GET', 'POST']) def add_message(uuid): content = request.get_json(silent=True) print (content) return jsonify('{"h" : "ok"}') @app.route('/api/asterixwrap', methods=['GET']) def api_asterixwrap(): sql = """USE AstxDB; select * from TableBT v; """ ads = AsterixDataSource() jsonobj = ads.execute(sql) return jsonify(jsonobj) def getNodeIds(category_list): _where = ' OR '.join(['user.category.nested.nested.level_2 = "{0}"'.format(x) for x in category_list]) sql=""" use bookstore_dp; select user.nodeID from ClassificationInfo user WHERE {0} """.format(_where) # where user.category.nested.nested.level_2 = "Education & Reference"; ads = AsterixDataSource(host="132.249.238.32") jsonobj = ads.execute(sql) return jsonobj @app.route("/api/highest_monthly_sales_by_category/<list>") def api_highest_monthly_sales_by_category(list): category_list = list.split(",") # print(category_list) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') #engine = create_engine('postgresql+psycopg2://postgres@45.79.91.219/MyBookStore') # conn = engine.connect() # _jlist = getNodeIds(category_list) # will be replaced by asterix call once connected to DB - the result will not change though print(_jlist) _inStr = convertToIn(_jlist) # print(_inStr) # sql = """ SELECT mon, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.productid, EXTRACT(MONTH from billdate) order by p.productid ) monthlysales WHERE category IN {0} GROUP By mon ORDER BY num_sold DESC """.format(_inStr) # print(sql) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'mon': int(result[0]), 'num_sold' : int(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() return (theresult_json) @app.route('/api/solrwrap', methods=['GET']) def api_solrwrap(): q = "*:*" ads = SolrDataSource() jsonobj = ads.execute(q) return jsonify(jsonobj) @app.route("/api/Top_Categories/<num_categories>/<months>") def Top_categories(num_categories, months): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid, EXTRACT(MONTH from billdate) order by p.nodeid ) monthlysales WHERE mon in ({0}) GROUP BY category ORDER BY num_sold DESC LIMIT ({1}) """.format(months,num_categories) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'category': result[0], 'num_sold': float(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() # return (results) # print(theresult_json) return (theresult_json) @app.route("/api/Discontinue_Stocking/<threshold>/<startyear>/<endyear>") def Discontinue_Stocking(threshold, startyear, endyear): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category FROM ( select EXTRACT(YEAR from o.billdate) as yr, p.nodeid as category, sum(o.numunits) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid , EXTRACT(YEAR from o.billdate) order by p.nodeid ) yearly_sales where books_sold < {0} AND (yr < {2} AND yr >= {1}) """.format(threshold,startyear, endyear) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'category': result[0]} l.append(d) theresult_json = json.dumps(l) conn.close() # return (results) # print(theresult_json) return (theresult_json) @app.route("/api/Downward_Sales/<season>") def
"""JSON serializer for objects not serializable by default json code""" if isinstance(obj, (datetime, date)): return obj.isoformat() raise TypeError ("Type %s not serializable" % type(obj))
identifier_body
Data_Exploration.py
(obj): """JSON serializer for objects not serializable by default json code""" if isinstance(obj, (datetime, date)): return obj.isoformat() raise TypeError ("Type %s not serializable" % type(obj)) @app.route("/api/web_method/<format>") def api_web_method(format): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select * from orderlines o, products p where o.productid = p.productid LIMIT 10 """ stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {} for item in request.args: c = request.args[item] print (c) d[c] = result[c] l.append(d) # theresult_json = json.dumps(l, default=json_serial) conn.close() return theresult_json @app.route("/api/correlation/<col1>/<col2>") def Correlation(col1, col2): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select corr(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/covariance/<col1>/<col2>", methods=['GET']) def Covariance(col1, col2): """Determine the covariance coefficient between two columns.""" engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select covar_samp(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/histogram/<groupby>/<count>") def Histogram(groupby, count): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT %s AS Group, count(%s) AS Count FROM orders Group by %s Order by count(%s) DESC """ % (groupby, count, groupby, count) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'Group': result[0], 'Count': result[1]} l.append(d) theresult_json = json.dumps(l) conn.close() return theresult_json @app.route('/api/add_message/<uuid>', methods=['GET', 'POST']) def add_message(uuid): content = request.get_json(silent=True) print (content) return jsonify('{"h" : "ok"}') @app.route('/api/asterixwrap', methods=['GET']) def api_asterixwrap(): sql = """USE AstxDB; select * from TableBT v; """ ads = AsterixDataSource() jsonobj = ads.execute(sql) return jsonify(jsonobj) def getNodeIds(category_list): _where = ' OR '.join(['user.category.nested.nested.level_2 = "{0}"'.format(x) for x in category_list]) sql=""" use bookstore_dp; select user.nodeID from ClassificationInfo user WHERE {0} """.format(_where) # where user.category.nested.nested.level_2 = "Education & Reference"; ads = AsterixDataSource(host="132.249.238.32") jsonobj = ads.execute(sql) return jsonobj @app.route("/api/highest_monthly_sales_by_category/<list>") def api_highest_monthly_sales_by_category(list): category_list = list.split(",") # print(category_list) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') #engine = create_engine('postgresql+psycopg2://postgres@45.79.91.219/MyBookStore') # conn = engine.connect() # _jlist = getNodeIds(category_list) # will be replaced by asterix call once connected to DB - the result will not change though print(_jlist) _inStr = convertToIn(_jlist) # print(_inStr) # sql = """ SELECT mon, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.productid, EXTRACT(MONTH from billdate) order by p.productid ) monthlysales WHERE category IN {0} GROUP By mon ORDER BY num_sold DESC """.format(_inStr) # print(sql) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'mon': int(result[0]), 'num_sold' : int(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() return (theresult_json) @app.route('/api/solrwrap', methods=['GET']) def api_solrwrap(): q = "*:*" ads = SolrDataSource() jsonobj = ads.execute(q) return jsonify(jsonobj) @app.route("/api/Top_Categories/<num_categories>/<months>") def Top_categories(num_categories, months): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid, EXTRACT(MONTH from billdate) order by p.nodeid ) monthlysales WHERE mon in ({0}) GROUP BY category ORDER BY num_sold DESC LIMIT ({1}) """.format(months,num_categories) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'category': result[0], 'num_sold': float(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() # return (results) # print(theresult_json) return (theresult_json) @app.route("/api/Discontinue_Stocking/<threshold>/<startyear>/<endyear>") def Discontinue_Stocking(threshold, startyear, endyear): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category FROM ( select EXTRACT(YEAR from o.billdate) as yr, p.nodeid as category, sum(o.numunits) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid , EXTRACT(YEAR from o.billdate) order by p.nodeid ) yearly_sales where books_sold < {0} AND (yr < {2} AND yr >= {1}) """.format(threshold,startyear, endyear) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'category': result[0]} l.append(d) theresult_json = json.dumps(l) conn.close() # return (results) # print(theresult_json) return (theresult_json) @app.route("/api/Downward_Sales
json_serial
identifier_name
Data_Exploration.py
app = Flask(__name__) @app.route("/") def Hello(): return "Hello World!" @app.route('/api/service', methods=['POST']) def api_service(): query = request.get_json(silent=True) # needs to change to reading from xml file xml = VirtualIntegrationSchema() web_session = WebSession(xml) return jsonify(web_session.get_result_sets(query)) def json_serial(obj): """JSON serializer for objects not serializable by default json code""" if isinstance(obj, (datetime, date)): return obj.isoformat() raise TypeError ("Type %s not serializable" % type(obj)) @app.route("/api/web_method/<format>") def api_web_method(format): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select * from orderlines o, products p where o.productid = p.productid LIMIT 10 """ stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {} for item in request.args: c = request.args[item] print (c) d[c] = result[c] l.append(d) # theresult_json = json.dumps(l, default=json_serial) conn.close() return theresult_json @app.route("/api/correlation/<col1>/<col2>") def Correlation(col1, col2): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select corr(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/covariance/<col1>/<col2>", methods=['GET']) def Covariance(col1, col2): """Determine the covariance coefficient between two columns.""" engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ select covar_samp(%s::int, %s::int) from orderlines o, products p where o.productid = p.productid """ %(col1, col2) stmt = text(sql) result = conn.execute(stmt) for row in result: print(row[0]) conn.close() return str(row[0]) @app.route("/api/histogram/<groupby>/<count>") def Histogram(groupby, count): engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT %s AS Group, count(%s) AS Count FROM orders Group by %s Order by count(%s) DESC """ % (groupby, count, groupby, count) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'Group': result[0], 'Count': result[1]} l.append(d) theresult_json = json.dumps(l) conn.close() return theresult_json @app.route('/api/add_message/<uuid>', methods=['GET', 'POST']) def add_message(uuid): content = request.get_json(silent=True) print (content) return jsonify('{"h" : "ok"}') @app.route('/api/asterixwrap', methods=['GET']) def api_asterixwrap(): sql = """USE AstxDB; select * from TableBT v; """ ads = AsterixDataSource() jsonobj = ads.execute(sql) return jsonify(jsonobj) def getNodeIds(category_list): _where = ' OR '.join(['user.category.nested.nested.level_2 = "{0}"'.format(x) for x in category_list]) sql=""" use bookstore_dp; select user.nodeID from ClassificationInfo user WHERE {0} """.format(_where) # where user.category.nested.nested.level_2 = "Education & Reference"; ads = AsterixDataSource(host="132.249.238.32") jsonobj = ads.execute(sql) return jsonobj @app.route("/api/highest_monthly_sales_by_category/<list>") def api_highest_monthly_sales_by_category(list): category_list = list.split(",") # print(category_list) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') #engine = create_engine('postgresql+psycopg2://postgres@45.79.91.219/MyBookStore') # conn = engine.connect() # _jlist = getNodeIds(category_list) # will be replaced by asterix call once connected to DB - the result will not change though print(_jlist) _inStr = convertToIn(_jlist) # print(_inStr) # sql = """ SELECT mon, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.productid, EXTRACT(MONTH from billdate) order by p.productid ) monthlysales WHERE category IN {0} GROUP By mon ORDER BY num_sold DESC """.format(_inStr) # print(sql) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'mon': int(result[0]), 'num_sold' : int(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() return (theresult_json) @app.route('/api/solrwrap', methods=['GET']) def api_solrwrap(): q = "*:*" ads = SolrDataSource() jsonobj = ads.execute(q) return jsonify(jsonobj) @app.route("/api/Top_Categories/<num_categories>/<months>") def Top_categories(num_categories, months): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category, sum(books_sold) AS num_sold FROM ( select EXTRACT(MONTH from o.billdate) as mon, p.nodeid as category, count(o.orderid) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid, EXTRACT(MONTH from billdate) order by p.nodeid ) monthlysales WHERE mon in ({0}) GROUP BY category ORDER BY num_sold DESC LIMIT ({1}) """.format(months,num_categories) stmt = text(sql) results = conn.execute(stmt) l = [] for result in results: d = {'category': result[0], 'num_sold': float(result[1])} l.append(d) theresult_json = json.dumps(l) conn.close() # return (results) # print(theresult_json) return (theresult_json) @app.route("/api/Discontinue_Stocking/<threshold>/<startyear>/<endyear>") def Discontinue_Stocking(threshold, startyear, endyear): # return jsonify({ "n" : num_categories, "m" : months}) engine = create_engine('postgresql+psycopg2://student:123456@132.249.238.27:5432/bookstore_dp') conn = engine.connect() sql = """ SELECT category FROM ( select EXTRACT(YEAR from o.billdate) as yr, p.nodeid as category, sum(o.numunits) as books_sold from orderlines as o, products as p where o.productid = p.productid AND o.totalprice > 0::money group by p.nodeid , EXTRACT(YEAR from o.billdate) order by p.nodeid ) yearly_sales where books_sold
from json import loads import psycopg2 from sqlalchemy import create_engine, text import pysolr from textblob import TextBlob as tb
random_line_split
ConfiguredResourceUploader.js
= ""; if (parsedResponse.ResultSet.Result.length !== 0) { script = parsedResponse.ResultSet.Result[0].script; //sort of a hack to get this code to work with generic nrg_config return values if(script == undefined ){ script = parsedResponse.ResultSet.Result[0].contents; } this.configs=YAHOO.lang.JSON.parse(script); this.showLinks(); } }, showLinks:function(){ $("a.uploadLink").each(function(value){ var type=$(this).attr('data-type'); var tempConfigs=new Array(); var props=$(this).attr('data-props');
if(tempConfigs.length>0){ if(value.dontHide){ $(value).color(value.defaultColor); $(value).css('cursor:pointer'); } $(this).click(function(){ XNAT.app.crUploader.show(this); return false; }); $(this).show(); }else{ if(!value.dontHide){ $(this).hide(); } } }); }, getConfigsByType:function(type){ var temp=new Array(); jq.each(this.configs,function(i1,v1){ if(v1.type==type){ temp.push(v1); } }); return temp; }, getAllConfigsByType:function(type, props){ var tmpConfigs=this.getConfigsByType(type); var typeInfo=window.available_elements.getByName(type); if(typeInfo!=undefined){ if(typeInfo.isSubjectAssessor){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:subjectAssessorData")); } if(typeInfo.isImageAssessor){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:imageAssessorData")); } if(typeInfo.isImageSession){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:imageSessionData")); } if(typeInfo.isImageScan){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:imageScanData")); } } var tempConfigs2=new Array(); //allow filtering of links jq.each(tmpConfigs,function(i1,v1){ if(props!=undefined && props!=null && v1.filter){ var filters=v1.filter.split(","); var matched=false; jq.each(filters,function (i2,v2){ if(!matched){ if((v2.trim()==props.trim())){ matched=true; } } }); if(matched){ tempConfigs2.push(v1); } }else{ tempConfigs2.push(v1); } }); return tempConfigs2; } } YAHOO.util.Event.onDOMReady(XNAT.app.crConfigs.load); XNAT.app.crUploader={ ready:false, show:function(config){ this.project=XNAT.app.crConfigs.project; this.type=$(config).attr('data-type'); this.uri=$(config).attr('data-uri'); this.props=$(config).attr('data-props'); this.updateForm(); XNAT.app.crUploader.dialog.render(document.body); XNAT.app.crUploader.dialog.show(); }, updateForm:function(obj){ var configs=XNAT.app.crConfigs.getAllConfigsByType(this.type,this.props); $('#cruSel').html(""); $('#cruSel').append($("<option value=''>SELECT</option>")); //render select options $.each(configs,function(index,value){ $('#cruSel').append($("<option value='" + value.name +"' data-message='" + ((value.description)?value.description:"") +"' data-level='" + ((value.level)?value.level:"") +"' data-overwrite='" + ((value.overwrite)?value.overwrite:"") +"' data-label='" + value.label +"' data-subdir='" + value.subdir +"'>" + value.name +"</option>")); }); if(this.registered==undefined){ this.registered=true; //add onchange event $('#cruSel').change(function(){ if($(this).val()!=""){ var desc=$("#cruSel option:selected").attr("data-message"); if(desc!=null && desc!=undefined){ $('#cruMsg').html(desc); }else{ $('#cruMsg').html(""); } }else{ $('#cruMsg').html(""); } }); } }, doUpload:function(allowOverwrite){ //executes the selected upload operation var frm=document.getElementById("cru_upload_frm"); if($('#cruSel').val()==""){ showMessage("page_body","Select resource","Please select a resource to upload."); return; } if(requireReason && frm.event_reason.value==""){ showMessage("page_body","Include justification.","Please include a justification for this upload."); return; } if(frm.upload_file.value==""){ showMessage("page_body","Select file.","Please use the file selector to choose a file to upload."); return; } YAHOO.util.Connect.setForm(frm,true); var callback={ upload:function(obj1){ closeModalPanel("cru_upl_mdl"); this.handleUpload(obj1); }, scope:this } var selector=$("#cruSel option:selected"); this.overwrite=selector.attr("data-overwrite"); this.level=selector.attr("data-level"); var params=""; params+="&event_type=WEB_FORM"; params+="&event_action=Uploaded "+ $(selector).text(); params+="&extract=true"; if(showReason && frm.event_reason.value!=""){ params+="&event_reason="+frm.event_reason.value; }else{ params+="&event_reason=standard upload"; } openModalPanel("cru_upl_mdl","Uploading Files"); var subdir=$(selector).attr('data-subdir'); if(subdir!=null && subdir!=undefined){ if(!subdir.startsWith("/")){ subdir="/"+subdir; } if(!subdir.endsWith("/")){ subdir=subdir+"/"; } }else{ subdir=""; } var filepath=frm.upload_file.value; while(filepath.indexOf("\\")>-1){ filepath=filepath.substring(filepath.indexOf("\\")+1); } filepath=subdir+filepath; if(allowOverwrite){ params+="&overwrite=true"; } var lvl=""; if(this.level!='default' && this.level!='' && this.level!=undefined){ lvl="/" + this.level; } YAHOO.util.Connect.asyncRequest('POST',this.uri + lvl +"/resources/"+ $(selector).attr('data-label') +"/files"+filepath+"?XNAT_CSRF=" + csrfToken +params,callback); }, handleUpload:function(response,o2,o3){ //handles the response form the upload operation //because this is a file upload, both successes and failures will use this method if(response.responseText==undefined || response.responseText=="" || response.responseText.match(/^<pre.*?><\/pre>$/)){ showMessage("page_body","Upload successful.","Your files have been successfully uploaded."); document.getElementById("cru_upload_frm").upload_file.value=""; if(window.viewer!=undefined && window.viewer.loading>0){ window.viewer.refreshCatalogs(); } }else if(response.responseText!=undefined && (response.responseText.indexOf("File already exists")>-1 || response.responseText.indexOf("duplicates")>-1)){ if(this.overwrite){ this.confirm("Duplicate files", "The uploaded files already exist on the server. Would you like to overwrite those files?",function(){ //yes this.hide(); XNAT.app.crUploader.doUpload(true); },function(){ //no this.hide(); XNAT.app.crUploader.dialog.hide(); }); }else{ showMessage("page_body","Failed upload.","The selected files already exist for this session."); } }else if(response.responseText==undefined){ //this block is for IE. For some reason the responseText is coming through as undefined. showMessage("page_body","Failed upload.","Unable to upload selected files."); }else{ showMessage("page_body","Failed upload.",response.responseText); } this.dialog.hide(); }, confirm : function (header, msg, handleYes, handleNo) { var dialog = new YAHOO.widget.SimpleDialog('widget_confirm', { visible:false, width: '20em', zIndex: 9998, close: false, fixedcenter: true, modal: true, draggable: true, constraintoviewport: true, icon: YAHOO.widget.SimpleDialog.ICON_WARN, buttons: [ { text: 'Yes', handler: handleYes}, { text: 'No', handler: handleNo, isDefault: true } ] }); dialog.setHeader(header); dialog.setBody(msg); dialog.cfg.queueProperty('icon', YAHOO.widget.SimpleDialog.ICON
var tempConfigs=XNAT.app.crConfigs.getAllConfigsByType(type,props)
random_line_split
ConfiguredResourceUploader.js
script = ""; if (parsedResponse.ResultSet.Result.length !== 0) { script = parsedResponse.ResultSet.Result[0].script; //sort of a hack to get this code to work with generic nrg_config return values if(script == undefined ){ script = parsedResponse.ResultSet.Result[0].contents; } this.configs=YAHOO.lang.JSON.parse(script); this.showLinks(); } }, showLinks:function(){ $("a.uploadLink").each(function(value){ var type=$(this).attr('data-type'); var tempConfigs=new Array(); var props=$(this).attr('data-props'); var tempConfigs=XNAT.app.crConfigs.getAllConfigsByType(type,props) if(tempConfigs.length>0){ if(value.dontHide){ $(value).color(value.defaultColor); $(value).css('cursor:pointer'); } $(this).click(function(){ XNAT.app.crUploader.show(this); return false; }); $(this).show(); }else{ if(!value.dontHide){ $(this).hide(); } } }); }, getConfigsByType:function(type){ var temp=new Array(); jq.each(this.configs,function(i1,v1){ if(v1.type==type){ temp.push(v1); } }); return temp; }, getAllConfigsByType:function(type, props){ var tmpConfigs=this.getConfigsByType(type); var typeInfo=window.available_elements.getByName(type); if(typeInfo!=undefined){ if(typeInfo.isSubjectAssessor){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:subjectAssessorData")); } if(typeInfo.isImageAssessor){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:imageAssessorData")); } if(typeInfo.isImageSession){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:imageSessionData")); } if(typeInfo.isImageScan){ tmpConfigs=tmpConfigs.concat(this.getConfigsByType("xnat:imageScanData")); } } var tempConfigs2=new Array(); //allow filtering of links jq.each(tmpConfigs,function(i1,v1){ if(props!=undefined && props!=null && v1.filter){ var filters=v1.filter.split(","); var matched=false; jq.each(filters,function (i2,v2){ if(!matched){ if((v2.trim()==props.trim())){ matched=true; } } }); if(matched){ tempConfigs2.push(v1); } }else{ tempConfigs2.push(v1); } }); return tempConfigs2; } } YAHOO.util.Event.onDOMReady(XNAT.app.crConfigs.load); XNAT.app.crUploader={ ready:false, show:function(config){ this.project=XNAT.app.crConfigs.project; this.type=$(config).attr('data-type'); this.uri=$(config).attr('data-uri'); this.props=$(config).attr('data-props'); this.updateForm(); XNAT.app.crUploader.dialog.render(document.body); XNAT.app.crUploader.dialog.show(); }, updateForm:function(obj){ var configs=XNAT.app.crConfigs.getAllConfigsByType(this.type,this.props); $('#cruSel').html(""); $('#cruSel').append($("<option value=''>SELECT</option>")); //render select options $.each(configs,function(index,value){ $('#cruSel').append($("<option value='" + value.name +"' data-message='" + ((value.description)?value.description:"") +"' data-level='" + ((value.level)?value.level:"") +"' data-overwrite='" + ((value.overwrite)?value.overwrite:"") +"' data-label='" + value.label +"' data-subdir='" + value.subdir +"'>" + value.name +"</option>")); }); if(this.registered==undefined){ this.registered=true; //add onchange event $('#cruSel').change(function(){ if($(this).val()!=""){ var desc=$("#cruSel option:selected").attr("data-message"); if(desc!=null && desc!=undefined){ $('#cruMsg').html(desc); }else{ $('#cruMsg').html(""); } }else{ $('#cruMsg').html(""); } }); } }, doUpload:function(allowOverwrite){ //executes the selected upload operation var frm=document.getElementById("cru_upload_frm"); if($('#cruSel').val()==""){ showMessage("page_body","Select resource","Please select a resource to upload."); return; } if(requireReason && frm.event_reason.value==""){ showMessage("page_body","Include justification.","Please include a justification for this upload."); return; } if(frm.upload_file.value==""){ showMessage("page_body","Select file.","Please use the file selector to choose a file to upload."); return; } YAHOO.util.Connect.setForm(frm,true); var callback={ upload:function(obj1){ closeModalPanel("cru_upl_mdl"); this.handleUpload(obj1); }, scope:this } var selector=$("#cruSel option:selected"); this.overwrite=selector.attr("data-overwrite"); this.level=selector.attr("data-level"); var params=""; params+="&event_type=WEB_FORM"; params+="&event_action=Uploaded "+ $(selector).text(); params+="&extract=true"; if(showReason && frm.event_reason.value!=""){ params+="&event_reason="+frm.event_reason.value; }else{ params+="&event_reason=standard upload"; } openModalPanel("cru_upl_mdl","Uploading Files"); var subdir=$(selector).attr('data-subdir'); if(subdir!=null && subdir!=undefined){ if(!subdir.startsWith("/")){ subdir="/"+subdir; } if(!subdir.endsWith("/")){ subdir=subdir+"/"; } }else{ subdir=""; } var filepath=frm.upload_file.value; while(filepath.indexOf("\\")>-1){ filepath=filepath.substring(filepath.indexOf("\\")+1); } filepath=subdir+filepath; if(allowOverwrite){ params+="&overwrite=true"; } var lvl=""; if(this.level!='default' && this.level!='' && this.level!=undefined){ lvl="/" + this.level; } YAHOO.util.Connect.asyncRequest('POST',this.uri + lvl +"/resources/"+ $(selector).attr('data-label') +"/files"+filepath+"?XNAT_CSRF=" + csrfToken +params,callback); }, handleUpload:function(response,o2,o3){ //handles the response form the upload operation //because this is a file upload, both successes and failures will use this method if(response.responseText==undefined || response.responseText=="" || response.responseText.match(/^<pre.*?><\/pre>$/)){ showMessage("page_body","Upload successful.","Your files have been successfully uploaded."); document.getElementById("cru_upload_frm").upload_file.value=""; if(window.viewer!=undefined && window.viewer.loading>0){ window.viewer.refreshCatalogs(); } }else if(response.responseText!=undefined && (response.responseText.indexOf("File already exists")>-1 || response.responseText.indexOf("duplicates")>-1)){ if(this.overwrite){ this.confirm("Duplicate files", "The uploaded files already exist on the server. Would you like to overwrite those files?",function(){ //yes this.hide(); XNAT.app.crUploader.doUpload(true); },function(){ //no this.hide(); XNAT.app.crUploader.dialog.hide(); }); }else{ showMessage("page_body","Failed upload.","The selected files already exist for this session."); } }else if(response.responseText==undefined){ //this block is for IE. For some reason the responseText is coming through as undefined. showMessage("page_body","Failed upload.","Unable to upload selected files."); }else
this.dialog.hide(); }, confirm : function (header, msg, handleYes, handleNo) { var dialog = new YAHOO.widget.SimpleDialog('widget_confirm', { visible:false, width: '20em', zIndex: 9998, close: false, fixedcenter: true, modal: true, draggable: true, constraintoviewport: true, icon: YAHOO.widget.SimpleDialog.ICON_WARN, buttons: [ { text: 'Yes', handler: handleYes}, { text: 'No', handler: handleNo, isDefault: true } ] }); dialog.setHeader(header); dialog.setBody(msg); dialog.cfg.queueProperty('icon', YAHOO.widget.SimpleDialog.ICON_HELP);
{ showMessage("page_body","Failed upload.",response.responseText); }
conditional_block
extractdicom.go
(dicomReader, nReaderBytes, nil) if err != nil { return nil, err } parsedData, err := SafelyDicomParse(p, dicom.ParseOptions{ DropPixelData: false, }) if parsedData == nil || err != nil { return nil, fmt.Errorf("Error reading zip: %v", err) } var rescaleSlope, rescaleIntercept, windowWidth, windowCenter float64 _, _, _, _ = rescaleSlope, rescaleIntercept, windowWidth, windowCenter var bitsAllocated, bitsStored, highBit uint16 _, _, _ = bitsAllocated, bitsStored, highBit var nOverlayRows, nOverlayCols int var img *image.Gray16 var imgRows, imgCols int var imgPixels []int var overlayPixels []int for _, elem := range parsedData.Elements { // The typical approach is to extract bitsAllocated, bitsStored, and the highBit // and to do transformations on the raw pixel values if elem.Tag == dicomtag.BitsAllocated { // log.Printf("BitsAllocated: %+v %T\n", elem.Value, elem.Value[0]) bitsAllocated = elem.Value[0].(uint16) } else if elem.Tag == dicomtag.BitsStored { // log.Printf("BitsStored: %+v %T\n", elem.Value, elem.Value[0]) bitsStored = elem.Value[0].(uint16) } else if elem.Tag == dicomtag.HighBit { // log.Printf("HighBit: %+v %T\n", elem.Value, elem.Value[0]) highBit = elem.Value[0].(uint16) } else if elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x0010}) == 0 { nOverlayRows = int(elem.Value[0].(uint16)) } else if elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x0011}) == 0 { nOverlayCols = int(elem.Value[0].(uint16)) } else if elem.Tag == dicomtag.Rows { imgRows = int(elem.Value[0].(uint16)) } else if elem.Tag == dicomtag.Columns { imgCols = int(elem.Value[0].(uint16)) } // If imgPixels is still uninitialized and we're on a rows or columns // tag, and both rows and columns are populated, initialize imgPixels' // backing array's capacity to the number of pixels in the image. if elem.Tag == dicomtag.Rows || elem.Tag == dicomtag.Columns && imgRows*imgCols > 0 && len(imgPixels) == 0 { imgPixels = make([]int, 0, imgRows*imgCols) } if elem.Tag == dicomtag.RescaleSlope { rescaleSlope, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.RescaleIntercept { rescaleIntercept, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowWidth { windowWidth, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowCenter { windowCenter, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if false { // Keeping for debugging if elem.Tag == dicomtag.PixelRepresentation { log.Printf("PixelRepresentation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleSlope { log.Printf("RescaleSlope: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleIntercept { log.Printf("RescaleIntercept: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleType { log.Printf("RescaleType: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PixelIntensityRelationship { log.Printf("PixelIntensityRelationship: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PhotometricInterpretation { log.Printf("PhotometricInterpretation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SamplesPerPixel { log.Printf("SamplesPerPixel: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.TransferSyntaxUID { log.Printf("TransferSyntaxUID: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SmallestImagePixelValue { log.Printf("SmallestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.LargestImagePixelValue { log.Printf("LargestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.VOILUTFunction { log.Printf("VOILUTFunction: %+v %T\n", elem.Value, elem.Value[0]) } } // Main image if elem.Tag == dicomtag.PixelData { data := elem.Value[0].(element.PixelDataInfo) for _, frame := range data.Frames { if frame.IsEncapsulated() { encImg, err := frame.GetImage() if err != nil { return nil, fmt.Errorf("Frame is encapsulated, which we did not expect. Additionally, %s", err.Error()) } // We're done, since it's not clear how to add an overlay return encImg, nil } for j := 0; j < len(frame.NativeData.Data); j++ { imgPixels = append(imgPixels, frame.NativeData.Data[j][0]) } } } // Extract the overlay, if it exists and we want it if opts.IncludeOverlay && elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x3000}) == 0 { // log.Println("Found the Overlay") // log.Println("Overlay bounds:", nOverlayCols, nOverlayRows) _, _ = nOverlayCols, nOverlayRows // We're in the overlay data for _, enclosed := range elem.Value { // There should be one enclosure, and it should contain a slice of // bytes, one byte per pixel. cellVals, ok := enclosed.([]byte) if !ok { continue } n_bits := 8 // Fill an array with zeroes, sized the nRows * nCols ( == n_bits * // len(cellVals) ) overlayPixels = make([]int, n_bits*len(cellVals), n_bits*len(cellVals)) // log.Println("Created a", len(overlayPixels), "array to hold the output") for i := range cellVals { byte_as_int := cellVals[i] for j := 0; j < n_bits; j++ { // Should be %cols and /cols -- row count is not necessary here overlayPixels[i*n_bits+j] = int((byte_as_int >> uint(j)) & 1) } } } } } // Identify the brightest pixel maxIntensity := 0 for _, v := range imgPixels { if v > maxIntensity { maxIntensity = v } } // Draw the image img = image.NewGray16(image.Rect(0, 0, imgCols, imgRows)) for j := 0; j < len(imgPixels); j++ { leVal := imgPixels[j] // Should be %cols and /cols -- row count is not necessary here switch opts.WindowScaling { case "pythonic": img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyPythonicWindowScaling(leVal, maxIntensity)}) case "raw": img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyNoWindowScaling(leVal)}) default: // "official" window scaling img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyOfficialWindowScaling(leVal, rescaleSlope, rescaleIntercept, windowWidth, windowCenter, bitsAllocated)}) }
random_line_split
extractdicom.go
{ imgRows = int(elem.Value[0].(uint16)) } else if elem.Tag == dicomtag.Columns { imgCols = int(elem.Value[0].(uint16)) } // If imgPixels is still uninitialized and we're on a rows or columns // tag, and both rows and columns are populated, initialize imgPixels' // backing array's capacity to the number of pixels in the image. if elem.Tag == dicomtag.Rows || elem.Tag == dicomtag.Columns && imgRows*imgCols > 0 && len(imgPixels) == 0 { imgPixels = make([]int, 0, imgRows*imgCols) } if elem.Tag == dicomtag.RescaleSlope { rescaleSlope, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.RescaleIntercept { rescaleIntercept, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowWidth { windowWidth, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowCenter { windowCenter, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if false { // Keeping for debugging if elem.Tag == dicomtag.PixelRepresentation { log.Printf("PixelRepresentation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleSlope { log.Printf("RescaleSlope: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleIntercept { log.Printf("RescaleIntercept: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleType { log.Printf("RescaleType: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PixelIntensityRelationship { log.Printf("PixelIntensityRelationship: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PhotometricInterpretation { log.Printf("PhotometricInterpretation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SamplesPerPixel { log.Printf("SamplesPerPixel: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.TransferSyntaxUID { log.Printf("TransferSyntaxUID: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SmallestImagePixelValue { log.Printf("SmallestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.LargestImagePixelValue { log.Printf("LargestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.VOILUTFunction { log.Printf("VOILUTFunction: %+v %T\n", elem.Value, elem.Value[0]) } } // Main image if elem.Tag == dicomtag.PixelData { data := elem.Value[0].(element.PixelDataInfo) for _, frame := range data.Frames { if frame.IsEncapsulated() { encImg, err := frame.GetImage() if err != nil { return nil, fmt.Errorf("Frame is encapsulated, which we did not expect. Additionally, %s", err.Error()) } // We're done, since it's not clear how to add an overlay return encImg, nil } for j := 0; j < len(frame.NativeData.Data); j++ { imgPixels = append(imgPixels, frame.NativeData.Data[j][0]) } } } // Extract the overlay, if it exists and we want it if opts.IncludeOverlay && elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x3000}) == 0 { // log.Println("Found the Overlay") // log.Println("Overlay bounds:", nOverlayCols, nOverlayRows) _, _ = nOverlayCols, nOverlayRows // We're in the overlay data for _, enclosed := range elem.Value { // There should be one enclosure, and it should contain a slice of // bytes, one byte per pixel. cellVals, ok := enclosed.([]byte) if !ok { continue } n_bits := 8 // Fill an array with zeroes, sized the nRows * nCols ( == n_bits * // len(cellVals) ) overlayPixels = make([]int, n_bits*len(cellVals), n_bits*len(cellVals)) // log.Println("Created a", len(overlayPixels), "array to hold the output") for i := range cellVals { byte_as_int := cellVals[i] for j := 0; j < n_bits; j++ { // Should be %cols and /cols -- row count is not necessary here overlayPixels[i*n_bits+j] = int((byte_as_int >> uint(j)) & 1) } } } } } // Identify the brightest pixel maxIntensity := 0 for _, v := range imgPixels { if v > maxIntensity { maxIntensity = v } } // Draw the image img = image.NewGray16(image.Rect(0, 0, imgCols, imgRows)) for j := 0; j < len(imgPixels); j++ { leVal := imgPixels[j] // Should be %cols and /cols -- row count is not necessary here switch opts.WindowScaling { case "pythonic": img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyPythonicWindowScaling(leVal, maxIntensity)}) case "raw": img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyNoWindowScaling(leVal)}) default: // "official" window scaling img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyOfficialWindowScaling(leVal, rescaleSlope, rescaleIntercept, windowWidth, windowCenter, bitsAllocated)}) } } // Draw the overlay if opts.IncludeOverlay && img != nil && overlayPixels != nil { // Iterate over the bytes. There will be 1 value for each cell. // So in a 1024x1024 overlay, you will expect 1,048,576 cells. for i, overlayValue := range overlayPixels { row := i / nOverlayCols col := i % nOverlayCols if overlayValue != 0 { img.SetGray16(col, row, color.White) } } } return img, err } // See 'Grayscale Image Display' under // https://dgobbi.github.io/vtk-dicom/doc/api/image_display.html . In addition, // we also scale the output so that it is appropriate for producing a 16-bit // grayscale image. E.g., if the native dicom is 8-bit, we still rescale the // output here for a 16-bit format. In the future, could produce 8-bit files // where possible, in which case this function would need to be changed. func ApplyOfficialWindowScaling(storedValue int, rescaleSlope, rescaleIntercept, windowWidth, windowCenter float64, bitsAllocated uint16) uint16
{ // 1: StoredValue to ModalityValue var modalityValue float64 if rescaleSlope == 0 { // Via https://dgobbi.github.io/vtk-dicom/doc/api/image_display.html : // For modalities such as ultrasound and MRI that do not have any units, // the RescaleSlope and RescaleIntercept are absent and the Modality // Values are equal to the Stored Values. modalityValue = float64(storedValue) } else { // Otherwise, we can apply the rescale slope and intercept to the stored // value. modalityValue = float64(storedValue)*rescaleSlope + rescaleIntercept } // 2: ModalityValue to WindowedValue // The key here is that we're using bitsAllocated (e.g., 16 bits) instead of // bitsStored (e.g., 11 bits) var grayLevels float64
identifier_body
extractdicom.go
(zipPath, dicomName string, includeOverlay bool) (image.Image, error) { return ExtractDicomFromGoogleStorage(zipPath, dicomName, includeOverlay, nil) } // ExtractDicomFromZipReader consumes a zip reader of the UK Biobank format, // finds the dicom of the desired name, and returns that image, with or without // the overlay (if any is present) based on includeOverlay. func ExtractDicomFromZipReader(rc *zip.Reader, dicomName string, includeOverlay bool) (image.Image, error) { for _, v := range rc.File { // Iterate over all of the dicoms in the zip til we find the one with // the desired name. This is reasonably efficient since we don't need to // read all of the data to find the right name. if v.Name != dicomName { continue } dicomReader, err := v.Open() if err != nil { return nil, err } defer dicomReader.Close() img, err := ExtractDicomFromReader(dicomReader, int64(v.UncompressedSize64), includeOverlay) return img, err } return nil, fmt.Errorf("Did not find the requested Dicom %s", dicomName) } // ExtractDicomFromReader operates on a reader that contains one DICOM. func ExtractDicomFromReader(dicomReader io.Reader, nReaderBytes int64, includeOverlay bool) (image.Image, error) { opts := []func(*ExtractDicomOptions){ func(opts *ExtractDicomOptions) { opts.IncludeOverlay = includeOverlay }, } return ExtractDicomFromReaderFuncOp(dicomReader, nReaderBytes, opts...) } func ExtractDicomFromReaderFuncOp(dicomReader io.Reader, nReaderBytes int64, options ...func(*ExtractDicomOptions)) (image.Image, error) { opts := &ExtractDicomOptions{} for _, opt := range options { opt(opts) } p, err := dicom.NewParser(dicomReader, nReaderBytes, nil) if err != nil { return nil, err } parsedData, err := SafelyDicomParse(p, dicom.ParseOptions{ DropPixelData: false, }) if parsedData == nil || err != nil { return nil, fmt.Errorf("Error reading zip: %v", err) } var rescaleSlope, rescaleIntercept, windowWidth, windowCenter float64 _, _, _, _ = rescaleSlope, rescaleIntercept, windowWidth, windowCenter var bitsAllocated, bitsStored, highBit uint16 _, _, _ = bitsAllocated, bitsStored, highBit var nOverlayRows, nOverlayCols int var img *image.Gray16 var imgRows, imgCols int var imgPixels []int var overlayPixels []int for _, elem := range parsedData.Elements { // The typical approach is to extract bitsAllocated, bitsStored, and the highBit // and to do transformations on the raw pixel values if elem.Tag == dicomtag.BitsAllocated { // log.Printf("BitsAllocated: %+v %T\n", elem.Value, elem.Value[0]) bitsAllocated = elem.Value[0].(uint16) } else if elem.Tag == dicomtag.BitsStored { // log.Printf("BitsStored: %+v %T\n", elem.Value, elem.Value[0]) bitsStored = elem.Value[0].(uint16) } else if elem.Tag == dicomtag.HighBit { // log.Printf("HighBit: %+v %T\n", elem.Value, elem.Value[0]) highBit = elem.Value[0].(uint16) } else if elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x0010}) == 0 { nOverlayRows = int(elem.Value[0].(uint16)) } else if elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x0011}) == 0 { nOverlayCols = int(elem.Value[0].(uint16)) } else if elem.Tag == dicomtag.Rows { imgRows = int(elem.Value[0].(uint16)) } else if elem.Tag == dicomtag.Columns { imgCols = int(elem.Value[0].(uint16)) } // If imgPixels is still uninitialized and we're on a rows or columns // tag, and both rows and columns are populated, initialize imgPixels' // backing array's capacity to the number of pixels in the image. if elem.Tag == dicomtag.Rows || elem.Tag == dicomtag.Columns && imgRows*imgCols > 0 && len(imgPixels) == 0 { imgPixels = make([]int, 0, imgRows*imgCols) } if elem.Tag == dicomtag.RescaleSlope { rescaleSlope, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.RescaleIntercept { rescaleIntercept, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowWidth { windowWidth, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowCenter { windowCenter, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if false { // Keeping for debugging if elem.Tag == dicomtag.PixelRepresentation { log.Printf("PixelRepresentation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleSlope { log.Printf("RescaleSlope: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleIntercept { log.Printf("RescaleIntercept: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleType { log.Printf("RescaleType: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PixelIntensityRelationship { log.Printf("PixelIntensityRelationship: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PhotometricInterpretation { log.Printf("PhotometricInterpretation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SamplesPerPixel { log.Printf("SamplesPerPixel: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.TransferSyntaxUID { log.Printf("TransferSyntaxUID: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SmallestImagePixelValue { log.Printf("SmallestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.LargestImagePixelValue { log.Printf("LargestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.VOILUTFunction { log.Printf("VOILUTFunction: %+v %T\n", elem.Value, elem.Value[0]) } } // Main image if elem.Tag == dicomtag.PixelData { data := elem.Value[0].(element.PixelDataInfo) for _, frame := range data.Frames { if frame.IsEncapsulated() { encImg, err := frame.GetImage() if err != nil { return nil, fmt.Errorf("Frame is encapsulated, which we did not expect. Additionally, %s", err.Error()) } // We're done, since it's not clear how to add an overlay return encImg, nil } for j := 0; j < len(frame.NativeData.Data); j++ { imgPixels = append(imgPixels, frame.NativeData.Data[j][0]) } } } // Extract the overlay, if it exists and we want it if opts.IncludeOverlay && elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x3000}) == 0 { // log.Println("Found the Overlay") // log.Println("Overlay bounds:", nOverlayCols, nOverlayRows) _, _ = nOverlayCols, nOverlayRows // We're in the overlay data for _, enclosed := range elem.Value { // There should be one enclosure, and it should contain a slice
ExtractDicomFromLocalFile
identifier_name
extractdicom.go
Bit uint16 _, _, _ = bitsAllocated, bitsStored, highBit var nOverlayRows, nOverlayCols int var img *image.Gray16 var imgRows, imgCols int var imgPixels []int var overlayPixels []int for _, elem := range parsedData.Elements { // The typical approach is to extract bitsAllocated, bitsStored, and the highBit // and to do transformations on the raw pixel values if elem.Tag == dicomtag.BitsAllocated { // log.Printf("BitsAllocated: %+v %T\n", elem.Value, elem.Value[0]) bitsAllocated = elem.Value[0].(uint16) } else if elem.Tag == dicomtag.BitsStored { // log.Printf("BitsStored: %+v %T\n", elem.Value, elem.Value[0]) bitsStored = elem.Value[0].(uint16) } else if elem.Tag == dicomtag.HighBit { // log.Printf("HighBit: %+v %T\n", elem.Value, elem.Value[0]) highBit = elem.Value[0].(uint16) } else if elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x0010}) == 0 { nOverlayRows = int(elem.Value[0].(uint16)) } else if elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x0011}) == 0 { nOverlayCols = int(elem.Value[0].(uint16)) } else if elem.Tag == dicomtag.Rows { imgRows = int(elem.Value[0].(uint16)) } else if elem.Tag == dicomtag.Columns { imgCols = int(elem.Value[0].(uint16)) } // If imgPixels is still uninitialized and we're on a rows or columns // tag, and both rows and columns are populated, initialize imgPixels' // backing array's capacity to the number of pixels in the image. if elem.Tag == dicomtag.Rows || elem.Tag == dicomtag.Columns && imgRows*imgCols > 0 && len(imgPixels) == 0 { imgPixels = make([]int, 0, imgRows*imgCols) } if elem.Tag == dicomtag.RescaleSlope { rescaleSlope, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.RescaleIntercept { rescaleIntercept, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowWidth { windowWidth, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if elem.Tag == dicomtag.WindowCenter { windowCenter, err = strconv.ParseFloat(elem.Value[0].(string), 64) if err != nil { log.Println(err) } } if false { // Keeping for debugging if elem.Tag == dicomtag.PixelRepresentation { log.Printf("PixelRepresentation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleSlope { log.Printf("RescaleSlope: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleIntercept { log.Printf("RescaleIntercept: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.RescaleType { log.Printf("RescaleType: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PixelIntensityRelationship { log.Printf("PixelIntensityRelationship: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.PhotometricInterpretation { log.Printf("PhotometricInterpretation: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SamplesPerPixel { log.Printf("SamplesPerPixel: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.TransferSyntaxUID { log.Printf("TransferSyntaxUID: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.SmallestImagePixelValue { log.Printf("SmallestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.LargestImagePixelValue { log.Printf("LargestImagePixelValue: %+v %T\n", elem.Value, elem.Value[0]) } else if elem.Tag == dicomtag.VOILUTFunction { log.Printf("VOILUTFunction: %+v %T\n", elem.Value, elem.Value[0]) } } // Main image if elem.Tag == dicomtag.PixelData { data := elem.Value[0].(element.PixelDataInfo) for _, frame := range data.Frames { if frame.IsEncapsulated() { encImg, err := frame.GetImage() if err != nil { return nil, fmt.Errorf("Frame is encapsulated, which we did not expect. Additionally, %s", err.Error()) } // We're done, since it's not clear how to add an overlay return encImg, nil } for j := 0; j < len(frame.NativeData.Data); j++ { imgPixels = append(imgPixels, frame.NativeData.Data[j][0]) } } } // Extract the overlay, if it exists and we want it if opts.IncludeOverlay && elem.Tag.Compare(dicomtag.Tag{Group: 0x6000, Element: 0x3000}) == 0 { // log.Println("Found the Overlay") // log.Println("Overlay bounds:", nOverlayCols, nOverlayRows) _, _ = nOverlayCols, nOverlayRows // We're in the overlay data for _, enclosed := range elem.Value { // There should be one enclosure, and it should contain a slice of // bytes, one byte per pixel. cellVals, ok := enclosed.([]byte) if !ok { continue } n_bits := 8 // Fill an array with zeroes, sized the nRows * nCols ( == n_bits * // len(cellVals) ) overlayPixels = make([]int, n_bits*len(cellVals), n_bits*len(cellVals)) // log.Println("Created a", len(overlayPixels), "array to hold the output") for i := range cellVals { byte_as_int := cellVals[i] for j := 0; j < n_bits; j++ { // Should be %cols and /cols -- row count is not necessary here overlayPixels[i*n_bits+j] = int((byte_as_int >> uint(j)) & 1) } } } } } // Identify the brightest pixel maxIntensity := 0 for _, v := range imgPixels { if v > maxIntensity { maxIntensity = v } } // Draw the image img = image.NewGray16(image.Rect(0, 0, imgCols, imgRows)) for j := 0; j < len(imgPixels); j++ { leVal := imgPixels[j] // Should be %cols and /cols -- row count is not necessary here switch opts.WindowScaling { case "pythonic": img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyPythonicWindowScaling(leVal, maxIntensity)}) case "raw": img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyNoWindowScaling(leVal)}) default: // "official" window scaling img.SetGray16(j%imgCols, j/imgCols, color.Gray16{Y: ApplyOfficialWindowScaling(leVal, rescaleSlope, rescaleIntercept, windowWidth, windowCenter, bitsAllocated)}) } } // Draw the overlay if opts.IncludeOverlay && img != nil && overlayPixels != nil { // Iterate over the bytes. There will be 1 value for each cell. // So in a 1024x1024 overlay, you will expect 1,048,576 cells. for i, overlayValue := range overlayPixels
{ row := i / nOverlayCols col := i % nOverlayCols if overlayValue != 0 { img.SetGray16(col, row, color.White) } }
conditional_block
lib.rs
when it goes out of scope //! ``` //! Pull from pool and `detach()` //! ``` //! let pool: MemoryPool<Vec<u8>> = MemoryPool::new(32, || Vec::with_capacity(4096)); //! let mut reusable_buff = pool.pull().unwrap(); // returns None when the pool is saturated //! reusable_buff.clear(); // clear the buff before using //! let (pool, reusable_buff) = reusable_buff.detach(); //! let mut s = String::from(reusable_buff); //! s.push_str("hello, world!"); //! pool.attach(s.into_bytes()); // reattach the buffer before reusable goes out of scope //! // reusable_buff is automatically returned to the pool when it goes out of scope //! ``` //! //! ## Using Across Threads //! //! You simply wrap the pool in a [`std::sync::Arc`] //! ``` //! let pool: Arc<MemoryPool<T>> = Arc::new(MemoryPool::new(cap, || T::new())); //! ``` //! //! # Warning //! //! Objects in the pool are not automatically reset, they are returned but NOT reset //! You may want to call `object.reset()` or `object.clear()` //! or any other equivalent for the object that you are using, after pulling from the pool //! //! [`std::sync::Arc`]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html mod multi_buf; mod semphore; pub use multi_buf::{MultiBuffer, GetSegs}; use crossbeam::channel; use std::ops::{Deref, DerefMut}; use parking_lot::{Mutex, Condvar}; use std::mem::{ManuallyDrop, forget}; use std::sync::Arc; use std::thread; use log::{trace}; pub use semphore::Semphore; use parking_lot::lock_api::MutexGuard; use futures::SinkExt; use std::thread::sleep; pub type Stack<T> = Vec<T>; pub struct PendingInfo<T> where T: Sync + Send + 'static { id: String, notifier: channel::Sender<T>, } pub struct WaitingInfo<T> where T: Sync + Send + 'static { id: String, //发送恢复命令 notifier: channel::Sender<T>, ///最低需要多少个内存单元才能恢复 min_request: usize, } pub struct MemoryPool<T> where T: Sync + Send + 'static { objects: (channel::Sender<T>, channel::Receiver<T>), // the one wait for data pending: Arc<Mutex<Vec<PendingInfo<Reusable<T>>>>>, ///those who is sleeping waiting: Arc<Mutex<Vec<WaitingInfo<Reusable<T>>>>>, run_block: Arc<Mutex<()>>, pending_block: Arc<Mutex<()>>, // recycle: (channel::Sender<Reusable<'a,T>>, channel::Receiver<Reusable<'a,T>>), } impl<T> MemoryPool<T> where T: Sync + Send + 'static { #[inline] pub fn new<F>(cap: usize, init: F) -> MemoryPool<T> where F: Fn() -> T, { // //println!("mempool remains:{}", cap); log::trace!("mempool remains:{}", cap); let mut objects = channel::unbounded(); for _ in 0..cap { &objects.0.send(init()); } MemoryPool { objects, pending: Arc::new(Mutex::new(Vec::new())), waiting: Arc::new(Mutex::new(Vec::new())), run_block: Arc::new(Mutex::new(())), pending_block: Arc::new(Mutex::new(())), } } #[inline] pub fn len(&self) -> usize { self.objects.1.len() } #[inline] pub fn is_empty(&self) -> bool { self.objects.1.is_empty() } #[inline] pub fn pending(&'static self, str: &str, sender: channel::Sender<Reusable<T>>, releasable: usize) -> (Option<Reusable<T>>, bool) { log::trace!("pending item:{}", str); let _x = self.pending_block.lock(); let ret = if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str); (Some(Reusable::new(&self, item)), false) /* } else if (self.pending.lock().len() == 0) { log::trace!("get should pend:{}", str); self.pending.lock().push(PendingInfo { id: String::from(str), notifier: sender.clone(), }); (None, false)*/ } else { let to_retry = { self.waiting.lock().len() * 60 + 2 }; log::trace!("try again :{} with retries backoff:{}", str, to_retry); for i in 0..to_retry { sleep(std::time::Duration::from_secs(1)); if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str); return (Some(Reusable::new(&self, item)), false); } } log::trace!("get should sleep :{}", str); self.waiting.lock().push(WaitingInfo { id: String::from(str), notifier: sender.clone(), min_request: releasable, }); (None, true) }; ret } #[inline] pub fn attach(&'static self, t: T) { let
elf.run_block.lock(); log::trace!("attach started<<<<<<<<<<<<<<<<"); log::trace!("recyled an item "); let mut wait_list = { self.waiting.lock() }; log::trace!("check waiting list ok :{}", wait_list.len()); if wait_list.len() > 0 && self.len() >= wait_list[0].min_request { log::trace!("remove ok<<<<<<<<<<<<<<< "); let item = wait_list.remove(0); log::trace!("start wakeup<<<<<<<<<<<<<<<<<<<"); //&wait_list.remove(0); self.objects.0.send(t).unwrap(); log::trace!("free cnts:{}, waking up {}/ with min req:{} now.... ", self.len(), item.id.clone(), item.min_request); for i in 0..item.min_request + 1 { item.notifier.send(Reusable::new(&self, self.objects.1.recv().unwrap())).unwrap_or_else(|e|{ log::warn!("notifier send failed"); }); } drop(item); // thread::spawn(move || { // item.notifier.send(()).unwrap(); // }); } else if self.pending.lock().len() > 0 { drop(wait_list); let pending_item = self.pending.lock().remove(0); log::trace!("fill pending:{}", pending_item.id); // thread::spawn(move || { // pending_item.notifier.send(()); // }); pending_item.notifier.send(Reusable::new(&self, t)); } else { // drop(wait_list); self.objects.0.send(t).unwrap(); log::trace!("push to queue:{}", self.len()); } } } pub struct Reusable<T> where T: Sync + Send + 'static { pool: &'static MemoryPool<T>, data: ManuallyDrop<T>, } impl<T> Reusable<T> where T: Sync + Send + 'static { #[inline] pub fn new(pool: &'static MemoryPool<T>, t: T) -> Self { Self { pool, data: ManuallyDrop::new(t), } } // #[inline] // pub fn detach(mut self) -> (&'a MemoryPool<T>, T) { // let ret = unsafe { (self.pool, self.take()) }; // forget(self); // ret // } // unsafe fn take(&mut self) -> T { ManuallyDrop::take(&mut self.data) } } impl<T> Deref for Reusable<T> where T: Sync + Send + 'static { type Target = T; #[inline] fn deref(&self) -> &Self::Target { &self.data } } impl<T> DerefMut for Reusable<T> where T: Sync + Send + 'static { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data } } impl<T> Drop for Reusable<T> where T: Sync + Send + 'static { #[inline] fn drop(&mut self) { unsafe { self.pool.attach(self.take()); } } } #[cfg(test)] mod tests { use crate::{MemoryPool, Reusable}; use std::mem::drop; use std::ops::DerefMut; use std::thread; use std::sync::Arc; // #[test] // fn pull() { // let pool = Arc::new(MemoryPool::<Vec<u8>>::new(3, || Vec::new())); // let pool2 = pool.clone(); // let t1 = thread::spawn(move ||{ // let object1 = pool.lock().pull(); // //println!("retain 1"); // thread::sleep(std::time::Duration::from_secs(1)); //
_x = s
identifier_name
lib.rs
when it goes out of scope //! ``` //! Pull from pool and `detach()` //! ``` //! let pool: MemoryPool<Vec<u8>> = MemoryPool::new(32, || Vec::with_capacity(4096)); //! let mut reusable_buff = pool.pull().unwrap(); // returns None when the pool is saturated //! reusable_buff.clear(); // clear the buff before using //! let (pool, reusable_buff) = reusable_buff.detach(); //! let mut s = String::from(reusable_buff); //! s.push_str("hello, world!"); //! pool.attach(s.into_bytes()); // reattach the buffer before reusable goes out of scope //! // reusable_buff is automatically returned to the pool when it goes out of scope //! ``` //! //! ## Using Across Threads //! //! You simply wrap the pool in a [`std::sync::Arc`] //! ``` //! let pool: Arc<MemoryPool<T>> = Arc::new(MemoryPool::new(cap, || T::new())); //! ``` //! //! # Warning //! //! Objects in the pool are not automatically reset, they are returned but NOT reset //! You may want to call `object.reset()` or `object.clear()` //! or any other equivalent for the object that you are using, after pulling from the pool //! //! [`std::sync::Arc`]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html mod multi_buf; mod semphore; pub use multi_buf::{MultiBuffer, GetSegs}; use crossbeam::channel; use std::ops::{Deref, DerefMut}; use parking_lot::{Mutex, Condvar}; use std::mem::{ManuallyDrop, forget}; use std::sync::Arc; use std::thread; use log::{trace}; pub use semphore::Semphore; use parking_lot::lock_api::MutexGuard; use futures::SinkExt; use std::thread::sleep; pub type Stack<T> = Vec<T>; pub struct PendingInfo<T> where T: Sync + Send + 'static { id: String, notifier: channel::Sender<T>, } pub struct WaitingInfo<T> where T: Sync + Send + 'static { id: String, //发送恢复命令 notifier: channel::Sender<T>, ///最低需要多少个内存单元才能恢复 min_request: usize, } pub struct MemoryPool<T> where T: Sync + Send + 'static { objects: (channel::Sender<T>, channel::Receiver<T>), // the one wait for data pending: Arc<Mutex<Vec<PendingInfo<Reusable<T>>>>>, ///those who is sleeping waiting: Arc<Mutex<Vec<WaitingInfo<Reusable<T>>>>>, run_block: Arc<Mutex<()>>, pending_block: Arc<Mutex<()>>, // recycle: (channel::Sender<Reusable<'a,T>>, channel::Receiver<Reusable<'a,T>>), } impl<T> MemoryPool<T> where T: Sync + Send + 'static { #[inline] pub fn new<F>(cap: usize, init: F) -> MemoryPool<T> where F: Fn() -> T, { // //println!("mempool remains:{}", cap); log::trace!("mempool remains:{}", cap); let mut objects = channel::unbounded(); for _ in 0..cap { &objects.0.send(init()); } MemoryPool { objects, pending: Arc::new(Mutex::new(Vec::new())), waiting: Arc::new(Mutex::new(Vec::new())), run_block: Arc::new(Mutex::new(())), pending_block: Arc::new(Mutex::new(())), } } #[inline] pub fn len(&self) -> usize { self.objects.1.len() } #[inline] pub fn is_empty(&self) -> bool { self.objects.1.is_empty() } #[inline] pub fn pending(&'static self, str: &str, sender: channel::Sender<Reusable<T>>, releasable: usize) -> (Option<Reusable<T>>, bool) { log::trace!("pending item:{}", str); let _x = self.pending_block.lock(); let ret = if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str
waiting.lock().len() * 60 + 2 }; log::trace!("try again :{} with retries backoff:{}", str, to_retry); for i in 0..to_retry { sleep(std::time::Duration::from_secs(1)); if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str); return (Some(Reusable::new(&self, item)), false); } } log::trace!("get should sleep :{}", str); self.waiting.lock().push(WaitingInfo { id: String::from(str), notifier: sender.clone(), min_request: releasable, }); (None, true) }; ret } #[inline] pub fn attach(&'static self, t: T) { let _x = self.run_block.lock(); log::trace!("attach started<<<<<<<<<<<<<<<<"); log::trace!("recyled an item "); let mut wait_list = { self.waiting.lock() }; log::trace!("check waiting list ok :{}", wait_list.len()); if wait_list.len() > 0 && self.len() >= wait_list[0].min_request { log::trace!("remove ok<<<<<<<<<<<<<<< "); let item = wait_list.remove(0); log::trace!("start wakeup<<<<<<<<<<<<<<<<<<<"); //&wait_list.remove(0); self.objects.0.send(t).unwrap(); log::trace!("free cnts:{}, waking up {}/ with min req:{} now.... ", self.len(), item.id.clone(), item.min_request); for i in 0..item.min_request + 1 { item.notifier.send(Reusable::new(&self, self.objects.1.recv().unwrap())).unwrap_or_else(|e|{ log::warn!("notifier send failed"); }); } drop(item); // thread::spawn(move || { // item.notifier.send(()).unwrap(); // }); } else if self.pending.lock().len() > 0 { drop(wait_list); let pending_item = self.pending.lock().remove(0); log::trace!("fill pending:{}", pending_item.id); // thread::spawn(move || { // pending_item.notifier.send(()); // }); pending_item.notifier.send(Reusable::new(&self, t)); } else { // drop(wait_list); self.objects.0.send(t).unwrap(); log::trace!("push to queue:{}", self.len()); } } } pub struct Reusable<T> where T: Sync + Send + 'static { pool: &'static MemoryPool<T>, data: ManuallyDrop<T>, } impl<T> Reusable<T> where T: Sync + Send + 'static { #[inline] pub fn new(pool: &'static MemoryPool<T>, t: T) -> Self { Self { pool, data: ManuallyDrop::new(t), } } // #[inline] // pub fn detach(mut self) -> (&'a MemoryPool<T>, T) { // let ret = unsafe { (self.pool, self.take()) }; // forget(self); // ret // } // unsafe fn take(&mut self) -> T { ManuallyDrop::take(&mut self.data) } } impl<T> Deref for Reusable<T> where T: Sync + Send + 'static { type Target = T; #[inline] fn deref(&self) -> &Self::Target { &self.data } } impl<T> DerefMut for Reusable<T> where T: Sync + Send + 'static { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data } } impl<T> Drop for Reusable<T> where T: Sync + Send + 'static { #[inline] fn drop(&mut self) { unsafe { self.pool.attach(self.take()); } } } #[cfg(test)] mod tests { use crate::{MemoryPool, Reusable}; use std::mem::drop; use std::ops::DerefMut; use std::thread; use std::sync::Arc; // #[test] // fn pull() { // let pool = Arc::new(MemoryPool::<Vec<u8>>::new(3, || Vec::new())); // let pool2 = pool.clone(); // let t1 = thread::spawn(move ||{ // let object1 = pool.lock().pull(); // //println!("retain 1"); // thread::sleep(std::time::Duration::from_secs(1)); // //
); (Some(Reusable::new(&self, item)), false) /* } else if (self.pending.lock().len() == 0) { log::trace!("get should pend:{}", str); self.pending.lock().push(PendingInfo { id: String::from(str), notifier: sender.clone(), }); (None, false)*/ } else { let to_retry = { self.
conditional_block
lib.rs
when it goes out of scope //! ``` //! Pull from pool and `detach()` //! ``` //! let pool: MemoryPool<Vec<u8>> = MemoryPool::new(32, || Vec::with_capacity(4096)); //! let mut reusable_buff = pool.pull().unwrap(); // returns None when the pool is saturated //! reusable_buff.clear(); // clear the buff before using //! let (pool, reusable_buff) = reusable_buff.detach(); //! let mut s = String::from(reusable_buff); //! s.push_str("hello, world!"); //! pool.attach(s.into_bytes()); // reattach the buffer before reusable goes out of scope //! // reusable_buff is automatically returned to the pool when it goes out of scope //! ``` //! //! ## Using Across Threads //! //! You simply wrap the pool in a [`std::sync::Arc`] //! ``` //! let pool: Arc<MemoryPool<T>> = Arc::new(MemoryPool::new(cap, || T::new())); //! ``` //! //! # Warning //! //! Objects in the pool are not automatically reset, they are returned but NOT reset //! You may want to call `object.reset()` or `object.clear()` //! or any other equivalent for the object that you are using, after pulling from the pool //! //! [`std::sync::Arc`]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html mod multi_buf; mod semphore; pub use multi_buf::{MultiBuffer, GetSegs}; use crossbeam::channel; use std::ops::{Deref, DerefMut}; use parking_lot::{Mutex, Condvar}; use std::mem::{ManuallyDrop, forget}; use std::sync::Arc; use std::thread; use log::{trace}; pub use semphore::Semphore; use parking_lot::lock_api::MutexGuard; use futures::SinkExt; use std::thread::sleep; pub type Stack<T> = Vec<T>; pub struct PendingInfo<T> where T: Sync + Send + 'static { id: String, notifier: channel::Sender<T>, } pub struct WaitingInfo<T> where T: Sync + Send + 'static { id: String, //发送恢复命令 notifier: channel::Sender<T>, ///最低需要多少个内存单元才能恢复 min_request: usize, } pub struct MemoryPool<T> where T: Sync + Send + 'static { objects: (channel::Sender<T>, channel::Receiver<T>), // the one wait for data pending: Arc<Mutex<Vec<PendingInfo<Reusable<T>>>>>, ///those who is sleeping waiting: Arc<Mutex<Vec<WaitingInfo<Reusable<T>>>>>, run_block: Arc<Mutex<()>>, pending_block: Arc<Mutex<()>>, // recycle: (channel::Sender<Reusable<'a,T>>, channel::Receiver<Reusable<'a,T>>), } impl<T> MemoryPool<T> where T: Sync + Send + 'static { #[inline] pub fn new<F>(cap: usize, init: F) -> MemoryPool<T> where F: Fn() -> T, { // //println!("mempool remains:
usize { self.objects.1.len() } #[inline] pub fn is_empty(&self) -> bool { self.objects.1.is_empty() } #[inline] pub fn pending(&'static self, str: &str, sender: channel::Sender<Reusable<T>>, releasable: usize) -> (Option<Reusable<T>>, bool) { log::trace!("pending item:{}", str); let _x = self.pending_block.lock(); let ret = if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str); (Some(Reusable::new(&self, item)), false) /* } else if (self.pending.lock().len() == 0) { log::trace!("get should pend:{}", str); self.pending.lock().push(PendingInfo { id: String::from(str), notifier: sender.clone(), }); (None, false)*/ } else { let to_retry = { self.waiting.lock().len() * 60 + 2 }; log::trace!("try again :{} with retries backoff:{}", str, to_retry); for i in 0..to_retry { sleep(std::time::Duration::from_secs(1)); if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str); return (Some(Reusable::new(&self, item)), false); } } log::trace!("get should sleep :{}", str); self.waiting.lock().push(WaitingInfo { id: String::from(str), notifier: sender.clone(), min_request: releasable, }); (None, true) }; ret } #[inline] pub fn attach(&'static self, t: T) { let _x = self.run_block.lock(); log::trace!("attach started<<<<<<<<<<<<<<<<"); log::trace!("recyled an item "); let mut wait_list = { self.waiting.lock() }; log::trace!("check waiting list ok :{}", wait_list.len()); if wait_list.len() > 0 && self.len() >= wait_list[0].min_request { log::trace!("remove ok<<<<<<<<<<<<<<< "); let item = wait_list.remove(0); log::trace!("start wakeup<<<<<<<<<<<<<<<<<<<"); //&wait_list.remove(0); self.objects.0.send(t).unwrap(); log::trace!("free cnts:{}, waking up {}/ with min req:{} now.... ", self.len(), item.id.clone(), item.min_request); for i in 0..item.min_request + 1 { item.notifier.send(Reusable::new(&self, self.objects.1.recv().unwrap())).unwrap_or_else(|e|{ log::warn!("notifier send failed"); }); } drop(item); // thread::spawn(move || { // item.notifier.send(()).unwrap(); // }); } else if self.pending.lock().len() > 0 { drop(wait_list); let pending_item = self.pending.lock().remove(0); log::trace!("fill pending:{}", pending_item.id); // thread::spawn(move || { // pending_item.notifier.send(()); // }); pending_item.notifier.send(Reusable::new(&self, t)); } else { // drop(wait_list); self.objects.0.send(t).unwrap(); log::trace!("push to queue:{}", self.len()); } } } pub struct Reusable<T> where T: Sync + Send + 'static { pool: &'static MemoryPool<T>, data: ManuallyDrop<T>, } impl<T> Reusable<T> where T: Sync + Send + 'static { #[inline] pub fn new(pool: &'static MemoryPool<T>, t: T) -> Self { Self { pool, data: ManuallyDrop::new(t), } } // #[inline] // pub fn detach(mut self) -> (&'a MemoryPool<T>, T) { // let ret = unsafe { (self.pool, self.take()) }; // forget(self); // ret // } // unsafe fn take(&mut self) -> T { ManuallyDrop::take(&mut self.data) } } impl<T> Deref for Reusable<T> where T: Sync + Send + 'static { type Target = T; #[inline] fn deref(&self) -> &Self::Target { &self.data } } impl<T> DerefMut for Reusable<T> where T: Sync + Send + 'static { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data } } impl<T> Drop for Reusable<T> where T: Sync + Send + 'static { #[inline] fn drop(&mut self) { unsafe { self.pool.attach(self.take()); } } } #[cfg(test)] mod tests { use crate::{MemoryPool, Reusable}; use std::mem::drop; use std::ops::DerefMut; use std::thread; use std::sync::Arc; // #[test] // fn pull() { // let pool = Arc::new(MemoryPool::<Vec<u8>>::new(3, || Vec::new())); // let pool2 = pool.clone(); // let t1 = thread::spawn(move ||{ // let object1 = pool.lock().pull(); // //println!("retain 1"); // thread::sleep(std::time::Duration::from_secs(1)); //
{}", cap); log::trace!("mempool remains:{}", cap); let mut objects = channel::unbounded(); for _ in 0..cap { &objects.0.send(init()); } MemoryPool { objects, pending: Arc::new(Mutex::new(Vec::new())), waiting: Arc::new(Mutex::new(Vec::new())), run_block: Arc::new(Mutex::new(())), pending_block: Arc::new(Mutex::new(())), } } #[inline] pub fn len(&self) ->
identifier_body
lib.rs
//! some_file.read_to_end(reusable_buff); //! // reusable_buff is automatically returned to the pool when it goes out of scope //! ``` //! Pull from pool and `detach()` //! ``` //! let pool: MemoryPool<Vec<u8>> = MemoryPool::new(32, || Vec::with_capacity(4096)); //! let mut reusable_buff = pool.pull().unwrap(); // returns None when the pool is saturated //! reusable_buff.clear(); // clear the buff before using //! let (pool, reusable_buff) = reusable_buff.detach(); //! let mut s = String::from(reusable_buff); //! s.push_str("hello, world!"); //! pool.attach(s.into_bytes()); // reattach the buffer before reusable goes out of scope //! // reusable_buff is automatically returned to the pool when it goes out of scope //! ``` //! //! ## Using Across Threads //! //! You simply wrap the pool in a [`std::sync::Arc`] //! ``` //! let pool: Arc<MemoryPool<T>> = Arc::new(MemoryPool::new(cap, || T::new())); //! ``` //! //! # Warning //! //! Objects in the pool are not automatically reset, they are returned but NOT reset //! You may want to call `object.reset()` or `object.clear()` //! or any other equivalent for the object that you are using, after pulling from the pool //! //! [`std::sync::Arc`]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html mod multi_buf; mod semphore; pub use multi_buf::{MultiBuffer, GetSegs}; use crossbeam::channel; use std::ops::{Deref, DerefMut}; use parking_lot::{Mutex, Condvar}; use std::mem::{ManuallyDrop, forget}; use std::sync::Arc; use std::thread; use log::{trace}; pub use semphore::Semphore; use parking_lot::lock_api::MutexGuard; use futures::SinkExt; use std::thread::sleep; pub type Stack<T> = Vec<T>; pub struct PendingInfo<T> where T: Sync + Send + 'static { id: String, notifier: channel::Sender<T>, } pub struct WaitingInfo<T> where T: Sync + Send + 'static { id: String, //发送恢复命令 notifier: channel::Sender<T>, ///最低需要多少个内存单元才能恢复 min_request: usize, } pub struct MemoryPool<T> where T: Sync + Send + 'static { objects: (channel::Sender<T>, channel::Receiver<T>), // the one wait for data pending: Arc<Mutex<Vec<PendingInfo<Reusable<T>>>>>, ///those who is sleeping waiting: Arc<Mutex<Vec<WaitingInfo<Reusable<T>>>>>, run_block: Arc<Mutex<()>>, pending_block: Arc<Mutex<()>>, // recycle: (channel::Sender<Reusable<'a,T>>, channel::Receiver<Reusable<'a,T>>), } impl<T> MemoryPool<T> where T: Sync + Send + 'static { #[inline] pub fn new<F>(cap: usize, init: F) -> MemoryPool<T> where F: Fn() -> T, { // //println!("mempool remains:{}", cap); log::trace!("mempool remains:{}", cap); let mut objects = channel::unbounded(); for _ in 0..cap { &objects.0.send(init()); } MemoryPool { objects, pending: Arc::new(Mutex::new(Vec::new())), waiting: Arc::new(Mutex::new(Vec::new())), run_block: Arc::new(Mutex::new(())), pending_block: Arc::new(Mutex::new(())), } } #[inline] pub fn len(&self) -> usize { self.objects.1.len() } #[inline] pub fn is_empty(&self) -> bool { self.objects.1.is_empty() } #[inline] pub fn pending(&'static self, str: &str, sender: channel::Sender<Reusable<T>>, releasable: usize) -> (Option<Reusable<T>>, bool) { log::trace!("pending item:{}", str); let _x = self.pending_block.lock(); let ret = if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str); (Some(Reusable::new(&self, item)), false) /* } else if (self.pending.lock().len() == 0) { log::trace!("get should pend:{}", str); self.pending.lock().push(PendingInfo { id: String::from(str), notifier: sender.clone(), }); (None, false)*/ } else { let to_retry = { self.waiting.lock().len() * 60 + 2 }; log::trace!("try again :{} with retries backoff:{}", str, to_retry); for i in 0..to_retry { sleep(std::time::Duration::from_secs(1)); if let Ok(item) = self.objects.1.try_recv() { log::trace!("get ok:{}", str); return (Some(Reusable::new(&self, item)), false); } } log::trace!("get should sleep :{}", str); self.waiting.lock().push(WaitingInfo { id: String::from(str), notifier: sender.clone(), min_request: releasable, }); (None, true) }; ret } #[inline] pub fn attach(&'static self, t: T) { let _x = self.run_block.lock(); log::trace!("attach started<<<<<<<<<<<<<<<<"); log::trace!("recyled an item "); let mut wait_list = { self.waiting.lock() }; log::trace!("check waiting list ok :{}", wait_list.len()); if wait_list.len() > 0 && self.len() >= wait_list[0].min_request { log::trace!("remove ok<<<<<<<<<<<<<<< "); let item = wait_list.remove(0); log::trace!("start wakeup<<<<<<<<<<<<<<<<<<<"); //&wait_list.remove(0); self.objects.0.send(t).unwrap(); log::trace!("free cnts:{}, waking up {}/ with min req:{} now.... ", self.len(), item.id.clone(), item.min_request); for i in 0..item.min_request + 1 { item.notifier.send(Reusable::new(&self, self.objects.1.recv().unwrap())).unwrap_or_else(|e|{ log::warn!("notifier send failed"); }); } drop(item); // thread::spawn(move || { // item.notifier.send(()).unwrap(); // }); } else if self.pending.lock().len() > 0 { drop(wait_list); let pending_item = self.pending.lock().remove(0); log::trace!("fill pending:{}", pending_item.id); // thread::spawn(move || { // pending_item.notifier.send(()); // }); pending_item.notifier.send(Reusable::new(&self, t)); } else { // drop(wait_list); self.objects.0.send(t).unwrap(); log::trace!("push to queue:{}", self.len()); } } } pub struct Reusable<T> where T: Sync + Send + 'static { pool: &'static MemoryPool<T>, data: ManuallyDrop<T>, } impl<T> Reusable<T> where T: Sync + Send + 'static { #[inline] pub fn new(pool: &'static MemoryPool<T>, t: T) -> Self { Self { pool, data: ManuallyDrop::new(t), } } // #[inline] // pub fn detach(mut self) -> (&'a MemoryPool<T>, T) { // let ret = unsafe { (self.pool, self.take()) }; // forget(self); // ret // } // unsafe fn take(&mut self) -> T { ManuallyDrop::take(&mut self.data) } } impl<T> Deref for Reusable<T> where T: Sync + Send + 'static { type Target = T; #[inline] fn deref(&self) -> &Self::Target { &self.data } } impl<T> DerefMut for Reusable<T> where T: Sync + Send + 'static { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data } } impl<T> Drop for Reusable<T> where T: Sync + Send + 'static { #[inline] fn drop(&mut self) { unsafe { self.pool.attach(self.take()); } } } #[cfg(test)] mod tests { use crate::{MemoryPool, Reusable}; use std::mem::drop; use std::ops::DerefMut; use std::thread; use std::sync::Arc; // #[test] // fn pull() { // let pool = Arc::new(Memory
//! let pool: MemoryPool<Vec<u8>> = MemoryPool::new(32, || Vec::with_capacity(4096)); //! let mut reusable_buff = pool.pull().unwrap(); // returns None when the pool is saturated //! reusable_buff.clear(); // clear the buff before using
random_line_split
counter.rs
AT_LOCK: Mutex<u32> = Mutex::new(42); } /// Configure event counter parameters. /// /// Unless specified, a counter is allocated in counting mode with a system-wide /// scope, recording events across all CPUs. /// /// ```no_run /// let config = CounterConfig::default().attach_to(vec![0]); /// /// let instr = config.allocate("inst_retired.any")?; /// let l1_hits = config.allocate("mem_load_uops_retired.l1_hit")?; /// # /// # Ok::<(), Error>(()) /// ``` #[derive(Debug, Default, Clone)] pub struct CounterBuilder { cpu: Option<i32>, pids: Option<Vec<i32>>, } impl CounterBuilder { /// Specify the CPU number that the PMC is to be allocated on. /// /// Defaults to all CPUs ([`CPU_ANY`]). pub fn set_cpu(self, cpu: i32) -> Self { Self { cpu: Some(cpu), ..self } } /// Attach a counter to the specified PID(s). /// /// When set, this causes the PMC to be allocated in process-scoped counting /// mode ([`pmc_mode_PMC_MODE_TC`] - see `man pmc`). /// /// # PID 0 /// /// PID 0 is a magic value, attaching to it causes the counter to be /// attached to the current (caller's) PID. pub fn attach_to(self, pids: impl Into<Vec<i32>>) -> Self { Self { pids: Some(pids.into()), ..self } } /// Allocate a PMC with the specified configuration, and attach to the /// target PIDs (if any). pub fn allocate(&self, event_spec: impl Into<String>) -> Result<Counter, Error> { Counter::new(event_spec, self.cpu, self.pids.clone()) } } #[derive(Debug)] struct AttachHandle { id: pmc_id_t, pid: i32, } impl Drop for AttachHandle { fn drop(&mut self) { // BUG: do not attempt to detach from pid 0 or risk live-locking the // machine. // // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=227041 // if self.pid != 0 { unsafe { pmc_detach(self.id, self.pid) }; } } } /// A handle to a running PMC counter. /// /// Dropping this handle causes the counter to stop recording events. pub struct Running<'a> { counter: &'a mut Counter, } impl<'a> Running<'a> { /// Read the current counter value. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let handle = counter.start()?; /// /// println!("instructions: {}", handle.read()?); /// # /// # Ok::<(), Error>(()) /// ``` pub fn read(&self) -> Result<u64, Error> { self.counter.read() } /// Set the value of the counter. pub fn set(&mut self, value: u64) -> Result<u64, Error> { self.counter.set(value) } /// Stop the counter from recording new events. pub fn stop(self) { drop(self) } } impl<'a> std::fmt::Display for Running<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.counter.fmt(f) } } impl<'a> Drop for Running<'a> { fn
(&mut self) { unsafe { pmc_stop(self.counter.id) }; } } /// An allocated PMC counter. /// /// Counters are initialised using the [`CounterBuilder`] type. /// /// ```no_run /// use std::{thread, time::Duration}; /// /// let instr = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let handle = instr.start()?; /// /// // Stop the counter after 5 seconds /// thread::sleep(Duration::from_secs(5)); /// handle.stop(); /// /// println!("instructions: {}", instr.read()?); /// # /// # Ok::<(), Error>(()) /// ``` #[derive(Debug)] pub struct Counter { id: pmc_id_t, attached: Option<Vec<AttachHandle>>, } impl Counter { fn new( event_spec: impl Into<String>, cpu: Option<i32>, pids: Option<Vec<i32>>, ) -> Result<Self, Error> { // If there's any pids, request a process counter, otherwise a // system-wide counter. let pmc_mode = if pids.is_none() { pmc_mode_PMC_MODE_SC } else { pmc_mode_PMC_MODE_TC }; // It appears pmc_allocate isn't thread safe, so take a lock while // calling it. let _guard = BIG_FAT_LOCK.lock().unwrap(); init_pmc_once()?; signal::check()?; let c_spec = CString::new(event_spec.into()).map_err(|_| new_error(ErrorKind::InvalidEventSpec))?; // Allocate the PMC let mut id = 0; if unsafe { pmc_allocate( c_spec.as_ptr(), pmc_mode, 0, cpu.unwrap_or(CPU_ANY), &mut id, 0, ) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(libc::EINVAL) => Err(new_os_error(ErrorKind::AllocInit)), _ => Err(new_os_error(ErrorKind::Unknown)), }; } // Initialise the counter so dropping it releases the PMC let mut c = Counter { id, attached: None }; // Attach to pids, if any, and collect handles so dropping them later // causes them to detach. // // The handles MUST be dropped before the Counter instance. if let Some(pids) = pids { let mut handles = vec![]; for pid in pids { if unsafe { pmc_attach(id, pid) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(libc::EBUSY) => unreachable!(), Some(libc::EEXIST) => Err(new_os_error(ErrorKind::AlreadyAttached)), Some(libc::EPERM) => Err(new_os_error(ErrorKind::Forbidden)), Some(libc::EINVAL) | Some(libc::ESRCH) => { Err(new_os_error(ErrorKind::BadTarget)) } _ => Err(new_os_error(ErrorKind::Unknown)), }; } handles.push(AttachHandle { id, pid }) } c.attached = Some(handles) } Ok(c) } /// Start this counter. /// /// The counter stops when the returned [`Running`] handle is dropped. #[must_use = "counter only runs until handle is dropped"] pub fn start(&mut self) -> Result<Running<'_>, Error> { signal::check()?; if unsafe { pmc_start(self.id) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(EDOOFUS) => Err(new_os_error(ErrorKind::LogFileRequired)), Some(libc::ENXIO) => Err(new_os_error(ErrorKind::BadScope)), _ => Err(new_os_error(ErrorKind::Unknown)), }; } Ok(Running { counter: self }) } /// Read the counter value. /// /// This call is valid for both running, stopped, and unused counters. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let r1 = counter.read()?; /// let r2 = counter.read()?; /// /// // A counter that is not running does not advance /// assert!(r2 == r1); /// # /// # Ok::<(), Error>(()) /// ``` pub fn read(&self) -> Result<u64, Error> { signal::check()?; let mut value: u64 = 0; if unsafe { pmc_read(self.id, &mut value) } != 0 { return Err(new_os_error(ErrorKind::Unknown)); } Ok(value) } /// Set an explicit counter value. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let r1 = counter.set(42)?; /// // The previous value is returned when setting a new value /// assert_eq!(r1, 0); /// /// // Reading the counter returns the value set /// let r2 = counter.read()?; /// assert_eq!(r2, 42); /// # /// # Ok::<(), Error>(()) /// ``` pub fn set(&mut self, value: u64) -> Result<u64,
drop
identifier_name
counter.rs
AT_LOCK: Mutex<u32> = Mutex::new(42); } /// Configure event counter parameters. /// /// Unless specified, a counter is allocated in counting mode with a system-wide /// scope, recording events across all CPUs. /// /// ```no_run /// let config = CounterConfig::default().attach_to(vec![0]); /// /// let instr = config.allocate("inst_retired.any")?; /// let l1_hits = config.allocate("mem_load_uops_retired.l1_hit")?; /// # /// # Ok::<(), Error>(()) /// ``` #[derive(Debug, Default, Clone)] pub struct CounterBuilder { cpu: Option<i32>, pids: Option<Vec<i32>>, } impl CounterBuilder { /// Specify the CPU number that the PMC is to be allocated on. /// /// Defaults to all CPUs ([`CPU_ANY`]). pub fn set_cpu(self, cpu: i32) -> Self { Self { cpu: Some(cpu), ..self } } /// Attach a counter to the specified PID(s). /// /// When set, this causes the PMC to be allocated in process-scoped counting /// mode ([`pmc_mode_PMC_MODE_TC`] - see `man pmc`). /// /// # PID 0 /// /// PID 0 is a magic value, attaching to it causes the counter to be /// attached to the current (caller's) PID. pub fn attach_to(self, pids: impl Into<Vec<i32>>) -> Self { Self { pids: Some(pids.into()), ..self } } /// Allocate a PMC with the specified configuration, and attach to the /// target PIDs (if any). pub fn allocate(&self, event_spec: impl Into<String>) -> Result<Counter, Error> { Counter::new(event_spec, self.cpu, self.pids.clone()) } } #[derive(Debug)] struct AttachHandle { id: pmc_id_t, pid: i32, } impl Drop for AttachHandle { fn drop(&mut self) { // BUG: do not attempt to detach from pid 0 or risk live-locking the // machine. // // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=227041 // if self.pid != 0 { unsafe { pmc_detach(self.id, self.pid) }; } } } /// A handle to a running PMC counter. /// /// Dropping this handle causes the counter to stop recording events. pub struct Running<'a> { counter: &'a mut Counter, } impl<'a> Running<'a> { /// Read the current counter value. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let handle = counter.start()?; /// /// println!("instructions: {}", handle.read()?); /// # /// # Ok::<(), Error>(()) /// ``` pub fn read(&self) -> Result<u64, Error> { self.counter.read() } /// Set the value of the counter. pub fn set(&mut self, value: u64) -> Result<u64, Error> { self.counter.set(value) } /// Stop the counter from recording new events. pub fn stop(self) { drop(self) } } impl<'a> std::fmt::Display for Running<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.counter.fmt(f) } } impl<'a> Drop for Running<'a> { fn drop(&mut self) { unsafe { pmc_stop(self.counter.id) }; } } /// An allocated PMC counter. /// /// Counters are initialised using the [`CounterBuilder`] type. /// /// ```no_run /// use std::{thread, time::Duration}; /// /// let instr = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let handle = instr.start()?; /// /// // Stop the counter after 5 seconds /// thread::sleep(Duration::from_secs(5)); /// handle.stop(); /// /// println!("instructions: {}", instr.read()?); /// # /// # Ok::<(), Error>(()) /// ``` #[derive(Debug)] pub struct Counter { id: pmc_id_t, attached: Option<Vec<AttachHandle>>, } impl Counter { fn new( event_spec: impl Into<String>, cpu: Option<i32>, pids: Option<Vec<i32>>, ) -> Result<Self, Error> { // If there's any pids, request a process counter, otherwise a // system-wide counter. let pmc_mode = if pids.is_none() { pmc_mode_PMC_MODE_SC } else { pmc_mode_PMC_MODE_TC }; // It appears pmc_allocate isn't thread safe, so take a lock while // calling it. let _guard = BIG_FAT_LOCK.lock().unwrap(); init_pmc_once()?; signal::check()?; let c_spec = CString::new(event_spec.into()).map_err(|_| new_error(ErrorKind::InvalidEventSpec))?; // Allocate the PMC let mut id = 0; if unsafe { pmc_allocate( c_spec.as_ptr(), pmc_mode, 0, cpu.unwrap_or(CPU_ANY), &mut id, 0, ) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(libc::EINVAL) => Err(new_os_error(ErrorKind::AllocInit)), _ => Err(new_os_error(ErrorKind::Unknown)), }; } // Initialise the counter so dropping it releases the PMC let mut c = Counter { id, attached: None }; // Attach to pids, if any, and collect handles so dropping them later // causes them to detach. // // The handles MUST be dropped before the Counter instance. if let Some(pids) = pids { let mut handles = vec![]; for pid in pids { if unsafe { pmc_attach(id, pid) } != 0
handles.push(AttachHandle { id, pid }) } c.attached = Some(handles) } Ok(c) } /// Start this counter. /// /// The counter stops when the returned [`Running`] handle is dropped. #[must_use = "counter only runs until handle is dropped"] pub fn start(&mut self) -> Result<Running<'_>, Error> { signal::check()?; if unsafe { pmc_start(self.id) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(EDOOFUS) => Err(new_os_error(ErrorKind::LogFileRequired)), Some(libc::ENXIO) => Err(new_os_error(ErrorKind::BadScope)), _ => Err(new_os_error(ErrorKind::Unknown)), }; } Ok(Running { counter: self }) } /// Read the counter value. /// /// This call is valid for both running, stopped, and unused counters. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let r1 = counter.read()?; /// let r2 = counter.read()?; /// /// // A counter that is not running does not advance /// assert!(r2 == r1); /// # /// # Ok::<(), Error>(()) /// ``` pub fn read(&self) -> Result<u64, Error> { signal::check()?; let mut value: u64 = 0; if unsafe { pmc_read(self.id, &mut value) } != 0 { return Err(new_os_error(ErrorKind::Unknown)); } Ok(value) } /// Set an explicit counter value. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let r1 = counter.set(42)?; /// // The previous value is returned when setting a new value /// assert_eq!(r1, 0); /// /// // Reading the counter returns the value set /// let r2 = counter.read()?; /// assert_eq!(r2, 42); /// # /// # Ok::<(), Error>(()) /// ``` pub fn set(&mut self, value: u64) -> Result<u64
{ return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(libc::EBUSY) => unreachable!(), Some(libc::EEXIST) => Err(new_os_error(ErrorKind::AlreadyAttached)), Some(libc::EPERM) => Err(new_os_error(ErrorKind::Forbidden)), Some(libc::EINVAL) | Some(libc::ESRCH) => { Err(new_os_error(ErrorKind::BadTarget)) } _ => Err(new_os_error(ErrorKind::Unknown)), }; }
conditional_block
counter.rs
_FAT_LOCK: Mutex<u32> = Mutex::new(42); } /// Configure event counter parameters. /// /// Unless specified, a counter is allocated in counting mode with a system-wide /// scope, recording events across all CPUs. /// /// ```no_run /// let config = CounterConfig::default().attach_to(vec![0]); /// /// let instr = config.allocate("inst_retired.any")?; /// let l1_hits = config.allocate("mem_load_uops_retired.l1_hit")?; /// # /// # Ok::<(), Error>(()) /// ``` #[derive(Debug, Default, Clone)] pub struct CounterBuilder { cpu: Option<i32>, pids: Option<Vec<i32>>, } impl CounterBuilder { /// Specify the CPU number that the PMC is to be allocated on. /// /// Defaults to all CPUs ([`CPU_ANY`]). pub fn set_cpu(self, cpu: i32) -> Self { Self { cpu: Some(cpu), ..self } } /// Attach a counter to the specified PID(s). /// /// When set, this causes the PMC to be allocated in process-scoped counting /// mode ([`pmc_mode_PMC_MODE_TC`] - see `man pmc`). /// /// # PID 0 /// /// PID 0 is a magic value, attaching to it causes the counter to be /// attached to the current (caller's) PID. pub fn attach_to(self, pids: impl Into<Vec<i32>>) -> Self { Self { pids: Some(pids.into()), ..self } } /// Allocate a PMC with the specified configuration, and attach to the /// target PIDs (if any). pub fn allocate(&self, event_spec: impl Into<String>) -> Result<Counter, Error> { Counter::new(event_spec, self.cpu, self.pids.clone()) } } #[derive(Debug)] struct AttachHandle { id: pmc_id_t, pid: i32, } impl Drop for AttachHandle { fn drop(&mut self) { // BUG: do not attempt to detach from pid 0 or risk live-locking the // machine. // // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=227041 // if self.pid != 0 { unsafe { pmc_detach(self.id, self.pid) }; } } } /// A handle to a running PMC counter. /// /// Dropping this handle causes the counter to stop recording events. pub struct Running<'a> { counter: &'a mut Counter, } impl<'a> Running<'a> { /// Read the current counter value. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let handle = counter.start()?; /// /// println!("instructions: {}", handle.read()?); /// # /// # Ok::<(), Error>(()) /// ``` pub fn read(&self) -> Result<u64, Error> { self.counter.read() } /// Set the value of the counter. pub fn set(&mut self, value: u64) -> Result<u64, Error> { self.counter.set(value) } /// Stop the counter from recording new events. pub fn stop(self) { drop(self) } } impl<'a> std::fmt::Display for Running<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.counter.fmt(f) } } impl<'a> Drop for Running<'a> { fn drop(&mut self) { unsafe { pmc_stop(self.counter.id) }; } } /// An allocated PMC counter. /// /// Counters are initialised using the [`CounterBuilder`] type. /// /// ```no_run /// use std::{thread, time::Duration}; /// /// let instr = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let handle = instr.start()?; /// /// // Stop the counter after 5 seconds /// thread::sleep(Duration::from_secs(5)); /// handle.stop(); /// /// println!("instructions: {}", instr.read()?); /// # /// # Ok::<(), Error>(()) /// ``` #[derive(Debug)] pub struct Counter { id: pmc_id_t, attached: Option<Vec<AttachHandle>>, } impl Counter { fn new( event_spec: impl Into<String>, cpu: Option<i32>, pids: Option<Vec<i32>>, ) -> Result<Self, Error> { // If there's any pids, request a process counter, otherwise a // system-wide counter. let pmc_mode = if pids.is_none() { pmc_mode_PMC_MODE_SC } else { pmc_mode_PMC_MODE_TC }; // It appears pmc_allocate isn't thread safe, so take a lock while // calling it. let _guard = BIG_FAT_LOCK.lock().unwrap(); init_pmc_once()?; signal::check()?;
// Allocate the PMC let mut id = 0; if unsafe { pmc_allocate( c_spec.as_ptr(), pmc_mode, 0, cpu.unwrap_or(CPU_ANY), &mut id, 0, ) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(libc::EINVAL) => Err(new_os_error(ErrorKind::AllocInit)), _ => Err(new_os_error(ErrorKind::Unknown)), }; } // Initialise the counter so dropping it releases the PMC let mut c = Counter { id, attached: None }; // Attach to pids, if any, and collect handles so dropping them later // causes them to detach. // // The handles MUST be dropped before the Counter instance. if let Some(pids) = pids { let mut handles = vec![]; for pid in pids { if unsafe { pmc_attach(id, pid) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(libc::EBUSY) => unreachable!(), Some(libc::EEXIST) => Err(new_os_error(ErrorKind::AlreadyAttached)), Some(libc::EPERM) => Err(new_os_error(ErrorKind::Forbidden)), Some(libc::EINVAL) | Some(libc::ESRCH) => { Err(new_os_error(ErrorKind::BadTarget)) } _ => Err(new_os_error(ErrorKind::Unknown)), }; } handles.push(AttachHandle { id, pid }) } c.attached = Some(handles) } Ok(c) } /// Start this counter. /// /// The counter stops when the returned [`Running`] handle is dropped. #[must_use = "counter only runs until handle is dropped"] pub fn start(&mut self) -> Result<Running<'_>, Error> { signal::check()?; if unsafe { pmc_start(self.id) } != 0 { return match io::Error::raw_os_error(&io::Error::last_os_error()) { Some(EDOOFUS) => Err(new_os_error(ErrorKind::LogFileRequired)), Some(libc::ENXIO) => Err(new_os_error(ErrorKind::BadScope)), _ => Err(new_os_error(ErrorKind::Unknown)), }; } Ok(Running { counter: self }) } /// Read the counter value. /// /// This call is valid for both running, stopped, and unused counters. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let r1 = counter.read()?; /// let r2 = counter.read()?; /// /// // A counter that is not running does not advance /// assert!(r2 == r1); /// # /// # Ok::<(), Error>(()) /// ``` pub fn read(&self) -> Result<u64, Error> { signal::check()?; let mut value: u64 = 0; if unsafe { pmc_read(self.id, &mut value) } != 0 { return Err(new_os_error(ErrorKind::Unknown)); } Ok(value) } /// Set an explicit counter value. /// /// ```no_run /// let mut counter = CounterConfig::default() /// .attach_to(vec![0]) /// .allocate("inst_retired.any")?; /// /// let r1 = counter.set(42)?; /// // The previous value is returned when setting a new value /// assert_eq!(r1, 0); /// /// // Reading the counter returns the value set /// let r2 = counter.read()?; /// assert_eq!(r2, 42); /// # /// # Ok::<(), Error>(()) /// ``` pub fn set(&mut self, value: u64) -> Result<u64, Error
let c_spec = CString::new(event_spec.into()).map_err(|_| new_error(ErrorKind::InvalidEventSpec))?;
random_line_split
sync.js
// for each op (colormap, pan, etc.) for(i=0; i<xops.length; i++){ // current op xop = xops[i]; this.syncs[xop] = this.syncs[xop] || []; ims = this.syncs[xop]; // add images not already in the list for(j=0; j<xlen; j++){ xim = xims[j]; if( $.inArray(xim, ims) < 0 ){ // add to list ims.push(xim); // we'll sync each new target image arr.push({im: this, xim: xim, xop: xop, xarg: null}); } } } // reciprocal sync'ing between all images? if( opts.reciprocate ){ JS9.Sync.reciprocating = true; opts.reciprocate = false; for(i=0, xim=this; i<xlen; i++){ xims.push(xim); xim = xims.shift(); JS9.Sync.sync.call(xim, xops, xims, opts); } delete JS9.Sync.reciprocating; } // use wcs for syncing if( JS9.notNull(opts.syncwcs) ){ this.tmp.syncwcs = opts.syncwcs; } else { this.tmp.syncwcs = JS9.globalOpts.syncWCS; } // sync target image, if necessary if( !JS9.Sync.reciprocating ){ // sync the target images JS9.Sync.xeqSync.call(this, arr); // flag we are ready to sync on user events JS9.Sync.ready = true; } }; // unsync one or more images // called in the image context JS9.Sync.unsync = function(ops, ims, opts){ let i, op, tims, xops, xims, xlen, xim; // sanity check if( !this.syncs ){ return; } // opts is optional opts = opts || {reciprocate: JS9.globalOpts.syncReciprocate}; // get regularized args xops = JS9.Sync.getOps.call(this, ops); xims = JS9.Sync.getIms.call(this, ims); xlen = xims.length; // reverse current image and target images? if( opts.reverse ){ delete opts.reverse; for(i=0; i<xlen; i++){ JS9.Sync.unsync.call(xims[i], xops, [this]); } return; } // for each op in this image ... for( op of Object.keys(this.syncs) ){ // skip this op if its not in the specified op list if( xops && $.inArray(op, xops) < 0 ){ continue; } // if no target images specified, delete the whole thing if( !xims ){ delete this.syncs[op]; } else { // get target image array for this image tims = this.syncs[op]; // for each target image ... for(i=tims.length-1; i>=0; i--){ // remove if it was specified for removal if( $.inArray(tims[i], xims) >= 0 ){ tims.splice(i, 1); } } // remove empty target image array if( !tims.length ){ delete this.syncs[op]; } } } // remove empty sink object from image if( !Object.keys(this.syncs).length ){ delete this.syncs; } // reciprocal sync'ing between all images? if( opts.reciprocate ){ JS9.Sync.reciprocating = true; opts.reciprocate = false; for(i=0, xim=this; i<xlen; i++){ xims.push(xim); xim = xims.shift(); JS9.Sync.unsync.call(xim, xops, xims, opts); } delete JS9.Sync.reciprocating; } }; // perform a sync action on target images using params from originating image // called in image context JS9.Sync.xeqSync = function(arr){ let i, j, k, obj, pos, wcscen, xim, xarr, xobj, xdata, key, diff; let mydata, myobj, myid, rarr, rstr, args, nflip; let displays = {}; const oval = JS9.globalOpts.xeqPlugins; const thisid = `${this.id}_${this.display.id}`; const regmatch = (r1, r2) => { // check for a target region with the same syncid as the current region if( !r1.data || !r1.data.syncid ){ return false; } if( !r2.data || !r2.data.syncid ){ return false; } return r1.data.syncid === r2.data.syncid; }; const calcFlip = (flip) => { let i, arr; let nx = 0; let ny = 0; let nflip = ""; arr = flip.split(""); for(i=0; i<arr.length; i++){ switch(arr[i]){ case "x": nx++; break; case "y": ny++; break; } } if( nx % 2 === 1 ){ nflip += "x"; } if( ny % 2 === 1 ){ nflip += "y"; } return nflip || ""; } // don't recurse! if( this.tmp.syncRunning ){ return; } this.tmp.syncRunning = true; // sync all target images with this operation (but swallow errors) try{ // do regions first to avoid problems with changes to the current image for(i=0; i<arr.length; i++){ obj = arr[i]; if( obj.xop === "regions" ){ arr.splice(i, 1); arr.unshift(obj); } } // process all operations for(i=0; i<arr.length; i++){ obj = arr[i]; xim = obj.xim; // don't recurse on target image if( xim.syncs ){ if( xim.tmp.syncRunning ){ continue; } xim.tmp.syncRunning = true; // if image is not displayed, we'll need to redisplay original if( xim !== xim.display.image ){ if( !displays[xim.display.id] ){ displays[xim.display.id] = xim.display.image; } } } try{ switch(obj.xop){ case "alignment": xim.alignPanZoom(this); break; case "colormap": xim.setColormap(this.params.colormap); break; case "contrastbias": xim.setColormap(this.params.contrast, this.params.bias); break; case "flip": if( this.params.flip != xim.params.flip ){ nflip = calcFlip(this.params.flip + xim.params.flip); xim.setFlip(nflip); } break; case "pan": pos = this.getPan(); if( this.tmp.syncwcs && this.validWCS() ){ wcscen = JS9.pix2wcs(this.raw.wcs, pos.ox, pos.oy); xim.setPan({wcs: wcscen}); } else { xim.setPan(pos.ox, pos.oy); } break; case "regions": // reset args args = []; xarr = null; if( obj.xarg ){ // region object of the current region args.push(obj.xarg); } else { // Try to sync all regions in the current image to // regions in the target. We will add regions which do // not exist in the target, and update those which do. if( !rarr ){ // get current regions, if necessary rarr = this.getShapes("regions", "all"); } // get regions in the target xarr = xim.getShapes("regions", "all"); // sync all current regions to the target, // either adding or updating for(j=0; j<rarr.length; j++){ // assume we will create a new region rarr[j].mode = "add"; // look through the target regions for(k=0; k<xarr.length; k++){ // if target matches the current region ... if( regmatch(xarr[k], rarr[j]) ){ // update it as an existing region rarr[j].mode = "update"; break; } } // we'll either add or update this region args.push(rarr[j]); } } // process all regions ... for(j=0; j<args.length; j++){ // get a copy of the regions object so we can change it myobj = $.extend(true, {}, args[j]); // get a sync id if( myobj.data && myobj.data.syncid ){ // reuse its syncid, if
{ delete opts.reverse; for(i=0; i<xlen; i++){ JS9.Sync.sync.call(xims[i], xops, [this]); } return; }
conditional_block
sync.js
if( xim && (xim.id !== this.id || (xim.display.id !== this.display.id)) ){ xims[j++] = xim; } } return xims; }; // sync image(s) when operations are performed on an originating image // called in the image context JS9.Sync.sync = function(...args){ let i, j, xop, xim, xops, xims, xlen; let [ops, ims, opts] = args; const arr = []; // make sure sink object exists this.syncs = this.syncs || {active: true}; // opts is optional opts = opts || {reciprocate: JS9.globalOpts.syncReciprocate}; if( typeof opts === "string" ){ try{ opts = JSON.parse(opts); } catch(e){ JS9.error(`can't parse sync opts: ${opts}`, e); } } // 1 boolean arg: turn on/off sync'ing if( args.length === 1 && typeof ops === "boolean" ){ this.syncs.active = ops; return; } // get regularized args xops = JS9.Sync.getOps.call(this, ops); xims = JS9.Sync.getIms.call(this, ims); xlen = xims.length; // reverse current image and target images? if( opts.reverse ){ delete opts.reverse; for(i=0; i<xlen; i++){ JS9.Sync.sync.call(xims[i], xops, [this]); } return; } // for each op (colormap, pan, etc.) for(i=0; i<xops.length; i++){ // current op xop = xops[i]; this.syncs[xop] = this.syncs[xop] || []; ims = this.syncs[xop]; // add images not already in the list for(j=0; j<xlen; j++){ xim = xims[j]; if( $.inArray(xim, ims) < 0 ){ // add to list ims.push(xim); // we'll sync each new target image arr.push({im: this, xim: xim, xop: xop, xarg: null}); } } } // reciprocal sync'ing between all images? if( opts.reciprocate ){ JS9.Sync.reciprocating = true; opts.reciprocate = false; for(i=0, xim=this; i<xlen; i++){ xims.push(xim); xim = xims.shift(); JS9.Sync.sync.call(xim, xops, xims, opts); } delete JS9.Sync.reciprocating; } // use wcs for syncing if( JS9.notNull(opts.syncwcs) ){ this.tmp.syncwcs = opts.syncwcs; } else { this.tmp.syncwcs = JS9.globalOpts.syncWCS; } // sync target image, if necessary if( !JS9.Sync.reciprocating ){ // sync the target images JS9.Sync.xeqSync.call(this, arr); // flag we are ready to sync on user events JS9.Sync.ready = true; } }; // unsync one or more images // called in the image context JS9.Sync.unsync = function(ops, ims, opts){ let i, op, tims, xops, xims, xlen, xim; // sanity check if( !this.syncs ){ return; } // opts is optional opts = opts || {reciprocate: JS9.globalOpts.syncReciprocate}; // get regularized args xops = JS9.Sync.getOps.call(this, ops); xims = JS9.Sync.getIms.call(this, ims); xlen = xims.length; // reverse current image and target images? if( opts.reverse ){ delete opts.reverse; for(i=0; i<xlen; i++){ JS9.Sync.unsync.call(xims[i], xops, [this]); } return; } // for each op in this image ... for( op of Object.keys(this.syncs) ){ // skip this op if its not in the specified op list if( xops && $.inArray(op, xops) < 0 ){ continue; } // if no target images specified, delete the whole thing if( !xims ){ delete this.syncs[op]; } else { // get target image array for this image tims = this.syncs[op]; // for each target image ... for(i=tims.length-1; i>=0; i--){ // remove if it was specified for removal if( $.inArray(tims[i], xims) >= 0 ){ tims.splice(i, 1); } } // remove empty target image array if( !tims.length ){ delete this.syncs[op]; } } } // remove empty sink object from image if( !Object.keys(this.syncs).length ){ delete this.syncs; } // reciprocal sync'ing between all images? if( opts.reciprocate ){ JS9.Sync.reciprocating = true; opts.reciprocate = false; for(i=0, xim=this; i<xlen; i++){ xims.push(xim); xim = xims.shift(); JS9.Sync.unsync.call(xim, xops, xims, opts); } delete JS9.Sync.reciprocating; } }; // perform a sync action on target images using params from originating image // called in image context JS9.Sync.xeqSync = function(arr){ let i, j, k, obj, pos, wcscen, xim, xarr, xobj, xdata, key, diff; let mydata, myobj, myid, rarr, rstr, args, nflip; let displays = {}; const oval = JS9.globalOpts.xeqPlugins; const thisid = `${this.id}_${this.display.id}`; const regmatch = (r1, r2) => { // check for a target region with the same syncid as the current region if( !r1.data || !r1.data.syncid ){ return false; } if( !r2.data || !r2.data.syncid ){ return false; } return r1.data.syncid === r2.data.syncid; }; const calcFlip = (flip) => { let i, arr; let nx = 0; let ny = 0; let nflip = ""; arr = flip.split(""); for(i=0; i<arr.length; i++){ switch(arr[i]){ case "x": nx++; break; case "y": ny++; break; } } if( nx % 2 === 1 ){ nflip += "x"; } if( ny % 2 === 1 ){ nflip += "y"; } return nflip || ""; } // don't recurse! if( this.tmp.syncRunning ){ return; } this.tmp.syncRunning = true; // sync all target images with this operation (but swallow errors) try{ // do regions first to avoid problems with changes to the current image for(i=0; i<arr.length; i++){ obj = arr[i]; if( obj.xop === "regions" ){ arr.splice(i, 1); arr.unshift(obj); } } // process all operations for(i=0; i<arr.length; i++){ obj = arr[i]; xim = obj.xim; // don't recurse on target image if( xim.syncs ){ if( xim.tmp.syncRunning ){ continue; } xim.tmp.syncRunning = true; // if image is not displayed, we'll need to redisplay original if( xim !== xim.display.image ){ if( !displays[xim.display.id] ){ displays[xim.display.id] = xim.display.image; } } } try{ switch(obj.xop){ case "alignment": xim.alignPanZoom(this); break; case "colormap": xim.setColormap(this.params.colormap); break; case "contrastbias": xim.setColormap(this.params.contrast, this.params.bias); break; case "flip": if( this.params.flip != xim.params.flip ){ nflip = calcFlip(this.params.flip + xim.params.flip); xim.setFlip(nflip); } break; case "pan": pos = this.getPan(); if( this.tmp.syncwcs && this.validWCS() ){ wcscen = JS9.pix2wcs(this.raw.wcs, pos.ox, pos.oy); xim.setPan({wcs: wcscen}); } else { xim.setPan(pos.ox, pos.oy); } break; case "regions": // reset args args = []; xarr = null; if( obj.xarg ){ // region object of the current region args.push(obj.xarg); } else { // Try
} else { xim = ims[i]; } // exclude the originating image
random_line_split
model_sql.go
+= fmt.Sprintf(" AND %s = ?", m.FieldAddAlias(field)) } whereValue = append(whereValue, data[field]) fileTitles = append(fileTitles, m.attr.Fields[m.attr.fieldIndexMap[field]].Title) } //非自增PK表,检查PK字段 if !m.attr.AutoInc { if where == "" { where = fmt.Sprintf("%s = ?", pk) } else { where = fmt.Sprintf("( %s ) OR ( %s )", where, fmt.Sprintf("%s = ?", pk)) } whereValue = append(whereValue, data[m.attr.Pk]) } db.Where(where, whereValue...) var total int64 if err := db.Count(&total).Error; err != nil { return err } else if total > 0 { return &Result{Message:fmt.Sprintf("记录已存在:【%s】存在重复", strings.Join(fileTitles, "、"))} } return nil } // 检查必填字段 func (m *Model) CheckRequiredValues(data map[string]interface{}) (err error) { fieldTitles := make([]string, 0) //非自增PK表,检查PK字段 if !m.attr.AutoInc { if cast.ToString(data[m.attr.Pk]) == "" { fieldTitles = append(fieldTitles, m.attr.Fields[m.attr.fieldIndexMap[m.attr.Pk]].Title) } } //检查配置中的必填字段 for _, field := range m.attr.Fields { if !field.Required { continue } if cast.ToString(data[field.Name]) == "" { fieldTitles = append(fieldTitles, field.Title) } } if len(fieldTitles) > 0 { return &Result{Message:fmt.Sprintf("【%s】 字段为必填项", strings.Join(fieldTitles, "、"))} } return } // 更新记录 func (m *Model) Updates(data map[string]interface{}, oldPkValue interface{}) (rowsAffected int64, err error) { //检查必填项 if err = m.CheckRequiredValues(data); err != nil { return } //检查重复记录 if err = m.CheckUnique(data, oldPkValue); err != nil { return } //更新数据 db := m.BaseDB(false) db.Where(fmt.Sprintf("`%s` = ?", m.attr.Pk), oldPkValue).Updates(data) return db.RowsAffected, db.Error } // 创建记录 func (m *Model) Create(data map[string]interface{}) (rowsAffected int64, err error) { //检查必填项 if err = m.CheckRequiredValues(data); err != nil { return } //检查重复记录 if err = m.CheckUnique(data, nil); err != nil { return } //创建数据 db := m.BaseDB(false).Create(data) return db.RowsAffected, db.Error } //保存记录(根据pk自动分析是update 或 create) func (m *Model) Save(data map[string]interface{}, oldPkValue interface{})(rowsAffected int64, err error) { if oldPkValue == nil { //创建 return m.Create(data) } else { //更新 return m.Updates(data, oldPkValue) } } //根据PK字段删除记录 func (m *Model) Delete(id interface{}) (rowsAffected int64, err error) { var delIds interface{} kind := reflect.TypeOf(id).Kind() symbol := "" if kind == reflect.Array || kind == reflect.Slice { symbol = "IN" delIds = id } else { symbol = "=" delIds = []interface{}{id} } db := m.BaseDB(false).Where(fmt.Sprintf("`%s` %s ?", m.attr.Pk, symbol), delIds).Delete(nil) return db.RowsAffected, db.Error } // 分析查询条件 (此批条件只作用于返回的db对象上,不会作用于模型的db上) // @param extraWhere 额外的查询条件 // @param searchValues 查询字段值 // @param notSearch 是否使用查询字段条件 func (m *Model) ParseWhere(db *gorm.DB, extraWhere []interface{}, searchValues map[string]interface{}, notSearch bool) *gorm.DB { var theDB *gorm.DB if db == nil { theDB = m.NewDB() } else { theDB = db.Where("") } //额外的查询条件 if extraWhere != nil { theDB.Where(extraWhere[0], extraWhere[1:]...) } // 模型各查询字段 if !notSearch { searchValues = m.ParseSearchValues(searchValues) for _, f := range m.attr.SearchFields { // 该查询字段未带条件配置 或 未传值,跳过 if _, ok := searchValues[f.Name]; !ok { continue } if f.Where == "" { f.Where = fmt.Sprintf("%s = ?", m.FieldAddAlias(f.Name)) f.Values = []string{"?"} } // 查询值与查询条件匹配 values := make([]interface{}, 0) if f.Between { //范围值 vType := reflect.TypeOf(searchValues[f.Name]).Kind() var vs []string if vType == reflect.Array || vType == reflect.Slice { vs = searchValues[f.Name].([]string) } else { vs = strings.Split(cast.ToString(searchValues[f.Name]), f.BetweenSep) } for i, v := range f.Values { if v == "?" { values = append(values, vs[i]) } else { values = append(values, strings.ReplaceAll(v, "?", vs[i])) } } } else { //单个值 for _, v := range f.Values { if v == "?" { values = append(values, searchValues[f.Name]) } else { values = append(values, strings.ReplaceAll(v, "?", cast.ToString(searchValues[f.Name]))) } } } theDB.Where(f.Where, values...) } } //受行权限控制的字段进行数据权限过滤 for fieldName, fromInfo := range m.attr.rowAuthFieldMap { if rowAuth, isAllAuth := option.ModelAuth.GetRowAuthCallback(fromInfo.FromName); !isAllAuth { theDB.Where(fmt.Sprintf("%s IN ?", m.FieldAddAlias(fieldName)), rowAuth) } } //如果自身也是行权限模型,则进行本身数据权限过滤 if m.attr.isRowAuth { if rowAuth, isAllAuth := option.ModelAuth.GetRowAuthCallback(m.attr.Name); !isAllAuth { theDB.Where(fmt.Sprintf("%s IN ?", m.FieldAddAlias(m.attr.Pk)), rowAuth) } } return theDB } //分析查询字段 // @param extraFields 额外附加的字段 // @return fields 最终需要查询的字段名数组 // @return footFields 汇总字段 func (m *Model) ParseFields(qo *QueryOption)(fields []string,footFields []string) { fields = make([]string, 0) footFields = make([]string, 0) //扩展字段 fields = append(fields, m.FieldsAddAlias(qo.ExtraFields)...) // 树型必备字段 if m.attr.IsTree { fields = append(fields, m.ParseTreeExtraField()...) } var modelFields []*ModelField if strings.ToLower(qo.useModelFiledType) == "edit" { modelFields = m.attr.editFields }else{ modelFields = m.attr.listFields } for _, field := range modelFields { //基础字段 fieldName := "" if field.Alias == "" { fieldName = m.FieldAddAlias(field.Name) } else if field.Alias != "" { fieldName = fmt.Sprintf("%s AS %s", field.Alias, field.Name) } fields = append(fields, fieldName) //汇总字段 if field.Foot != "" { footFields = append(footFields, fmt.Sprintf("%s AS %s", field.Foot, field.Name)) } } return } // 分析kv字段数组 (仅对通过NewConfigModel创建的模型有效) // @param kvName kv配置项名 // @param extraFields 额外附加的字段 // @return fields 最终需要查询的KV字段名数组 func (m *Model) ParseKvFields(kvName string, extraFields []string) (fields []string) { fields = make([]string, 0) // kv配置中的字段 kv, ok := ModelKv{}, false if kv, ok = m.attr.Kvs[kvName]; !ok { return } //keySep := fmt.Sprintf(",'%s',", kv.KeySep) //valueSep := fmt.Sprintf(",'%s',", kv.ValueSep) keyField := fmt.Sprintf("%s AS `__mc_key`", m.FieldAddAlias(kv.KeyField)) valueField := fmt.Sprintf("%s AS `__mc_value`", m.FieldAddAlias(kv.ValueField))
fields = append(fields, keyField, valueField) // 树型必备字段 if m.attr.IsTree {
random_line_split
model_sql.go
//查询 data := make([]map[string]interface{}, 0) if err = theDB.Select(fields).Find(&data).Error; err != nil { return } //处理结果 desc = make(Kvs) for i, v := range data { key := cast.ToString(v["__mc_key"]) //树形 if m.attr.IsTree && qo.ReturnPath { key = cast.ToString(v[m.attr.Tree.PathField]) } indent := "" if qo.TreeIndent == nil { indent = m.attr.Tree.Indent } else { indent = *qo.TreeIndent } if m.attr.IsTree && indent != "" { //树形名称字段加前缀 data[i]["__mc_value"] = nString(indent, cast.ToInt(data[i]["__mc_level"])-1) + cast.ToString(data[i]["__mc_value"]) } desc[key] = v } return } // 获取一条编辑数据 func (m *Model) TakeForEdit(qo *QueryOption) (desc map[string]interface{}, exist bool, err error) { indent := "" qo.NotConvertFromValue = true qo.NotSearch = true qo.TreeIndent = &indent qo.useModelFiledType = "edit" return m.Take(qo) } // 获取一条list数据 func (m *Model) Take(qo *QueryOption) (desc map[string]interface{}, exist bool, err error) { qo.PageSize = 1 qo.Page = 1 qo.NotTotal = true qo.NotFoot = true if data, _, _, err := m.Find(qo); err != nil { return nil, false, err }else if len(data) < 0 { return nil, false, nil }else{ return data[0], true, nil } } // 获取list数据列表 func (m *Model) Find(qo *QueryOption) (desc []map[string]interface{}, foot map[string]interface{}, total int64, err error) { //检查选项 if qo == nil { qo = &QueryOption{} } //分析查询的字段 fields, footFields := m.ParseFields(qo) if fields == nil || len(fields) <= 0 { return } //分析查询条件 theDB := m.ParseWhere(qo.DB, qo.ExtraWhere, qo.Values, qo.NotSearch) //排序 if qo.Order != "" { theDB.Order(qo.Order) } else if m.attr.Order != "" { theDB.Order(m.attr.Order) } //分页信息 offset, limit := getOffsetLimit(qo.Page, qo.PageSize) //查询 desc = make([]map[string]interface{}, 0) db := theDB.Session(&gorm.Session{}) db.Offset(offset).Limit(limit).Select(fields).Find(&desc) if !qo.NotTotal { db = theDB.Session(&gorm.Session{}) db.Count(&total) } if theDB.Error != nil { err = theDB.Error return } //汇总 if !qo.NotFoot && footFields != nil && len(footFields) > 0 { foot = make(map[string]interface{}) if err = theDB.Select(footFields).Offset(0).Limit(1).Take(&foot).Error; err != nil { return } } err = m.ProcessData(desc, qo) return } // 判断是否已有重复数据 func (m *Model) CheckUnique(data map[string]interface{}, oldPkValue interface{})(err error) { //如果没有设置唯一字段,且主键是自增时,直接返回不重复 if (m.attr.UniqueFields == nil || len(m.attr.UniqueFields) <= 0) && m.attr.AutoInc { return } db := m.BaseDB(true) pk := m.FieldAddAlias(m.attr.Pk) fileTitles := make([]string, 0) if oldPkValue != nil { db.Where(fmt.Sprintf("%s <> ?", pk), oldPkValue) fileTitles = append(fileTitles, m.attr.Fields[m.attr.fieldIndexMap[pk]].Title) } where := "" whereValue := make([]interface{}, 0) //检查唯一字段 for _, field := range m.attr.UniqueFields { if where == "" { where += fmt.Sprintf(" %s = ?", m.FieldAddAlias(field)) } else { where += fmt.Sprintf(" AND %s = ?", m.FieldAddAlias(field)) } whereValue = append(whereValue, data[field]) fileTitles = append(fileTitles, m.attr.Fields[m.attr.fieldIndexMap[field]].Title) } //非自增PK表,检查PK字段 if !m.attr.AutoInc { if where == "" { where = fmt.Sprintf("%s = ?", pk) } else { where = fmt.Sprintf("( %s ) OR ( %s )", where, fmt.Sprintf("%s = ?", pk)) } whereValue = append(whereValue, data[m.attr.Pk]) } db.Where(where, whereValue...) var total int64 if err := db.Count(&total).Error; err != nil { return er
填字段 func (m *Model) CheckRequiredValues(data map[string]interface{}) (err error) { fieldTitles := make([]string, 0) //非自增PK表,检查PK字段 if !m.attr.AutoInc { if cast.ToString(data[m.attr.Pk]) == "" { fieldTitles = append(fieldTitles, m.attr.Fields[m.attr.fieldIndexMap[m.attr.Pk]].Title) } } //检查配置中的必填字段 for _, field := range m.attr.Fields { if !field.Required { continue } if cast.ToString(data[field.Name]) == "" { fieldTitles = append(fieldTitles, field.Title) } } if len(fieldTitles) > 0 { return &Result{Message:fmt.Sprintf("【%s】 字段为必填项", strings.Join(fieldTitles, "、"))} } return } // 更新记录 func (m *Model) Updates(data map[string]interface{}, oldPkValue interface{}) (rowsAffected int64, err error) { //检查必填项 if err = m.CheckRequiredValues(data); err != nil { return } //检查重复记录 if err = m.CheckUnique(data, oldPkValue); err != nil { return } //更新数据 db := m.BaseDB(false) db.Where(fmt.Sprintf("`%s` = ?", m.attr.Pk), oldPkValue).Updates(data) return db.RowsAffected, db.Error } // 创建记录 func (m *Model) Create(data map[string]interface{}) (rowsAffected int64, err error) { //检查必填项 if err = m.CheckRequiredValues(data); err != nil { return } //检查重复记录 if err = m.CheckUnique(data, nil); err != nil { return } //创建数据 db := m.BaseDB(false).Create(data) return db.RowsAffected, db.Error } //保存记录(根据pk自动分析是update 或 create) func (m *Model) Save(data map[string]interface{}, oldPkValue interface{})(rowsAffected int64, err error) { if oldPkValue == nil { //创建 return m.Create(data) } else { //更新 return m.Updates(data, oldPkValue) } } //根据PK字段删除记录 func (m *Model) Delete(id interface{}) (rowsAffected int64, err error) { var delIds interface{} kind := reflect.TypeOf(id).Kind() symbol := "" if kind == reflect.Array || kind == reflect.Slice { symbol = "IN" delIds = id } else { symbol = "=" delIds = []interface{}{id} } db := m.BaseDB(false).Where(fmt.Sprintf("`%s` %s ?", m.attr.Pk, symbol), delIds).Delete(nil) return db.RowsAffected, db.Error } // 分析查询条件 (此批条件只作用于返回的db对象上,不会作用于模型的db上) // @param extraWhere 额外的查询条件 // @param searchValues 查询字段值 // @param notSearch 是否使用查询字段条件 func (m *Model) ParseWhere(db *gorm.DB, extraWhere []interface{}, searchValues map[string]interface{}, notSearch bool) *gorm.DB { var theDB *gorm.DB if db == nil { theDB = m.NewDB() } else { theDB = db.Where("") } //额外的查询条件 if extraWhere != nil { theDB.Where(extraWhere[0], extraWhere[1:]...) } // 模型各查询字段 if !notSearch { searchValues = m.ParseSearchValues(searchValues) for _, f := range m.attr.SearchFields { // 该查询字段未带条件配置 或 未传值,跳过 if _, ok := searchValues[f.Name]; !ok
r } else if total > 0 { return &Result{Message:fmt.Sprintf("记录已存在:【%s】存在重复", strings.Join(fileTitles, "、"))} } return nil } // 检查必
conditional_block
model_sql.go
.attr.Pk, symbol), delIds).Delete(nil) return db.RowsAffected, db.Error } // 分析查询条件 (此批条件只作用于返回的db对象上,不会作用于模型的db上) // @param extraWhere 额外的查询条件 // @param searchValues 查询字段值 // @param notSearch 是否使用查询字段条件 func (m *Model) ParseWhere(db *gorm.DB, extraWhere []interface{}, searchValues map[string]interface{}, notSearch bool) *gorm.DB { var theDB *gorm.DB if db == nil { theDB = m.NewDB() } else { theDB = db.Where("") } //额外的查询条件 if extraWhere != nil { theDB.Where(extraWhere[0], extraWhere[1:]...) } // 模型各查询字段 if !notSearch { searchValues = m.ParseSearchValues(searchValues) for _, f := range m.attr.SearchFields { // 该查询字段未带条件配置 或 未传值,跳过 if _, ok := searchValues[f.Name]; !ok { continue } if f.Where == "" { f.Where = fmt.Sprintf("%s = ?", m.FieldAddAlias(f.Name)) f.Values = []string{"?"} } // 查询值与查询条件匹配 values := make([]interface{}, 0) if f.Between { //范围值 vType := reflect.TypeOf(searchValues[f.Name]).Kind() var vs []string if vType == reflect.Array || vType == reflect.Slice { vs = searchValues[f.Name].([]string) } else { vs = strings.Split(cast.ToString(searchValues[f.Name]), f.BetweenSep) } for i, v := range f.Values { if v == "?" { values = append(values, vs[i]) } else { values = append(values, strings.ReplaceAll(v, "?", vs[i])) } } } else { //单个值 for _, v := range f.Values { if v == "?" { values = append(values, searchValues[f.Name]) } else { values = append(values, strings.ReplaceAll(v, "?", cast.ToString(searchValues[f.Name]))) } } } theDB.Where(f.Where, values...) } } //受行权限控制的字段进行数据权限过滤 for fieldName, fromInfo := range m.attr.rowAuthFieldMap { if rowAuth, isAllAuth := option.ModelAuth.GetRowAuthCallback(fromInfo.FromName); !isAllAuth { theDB.Where(fmt.Sprintf("%s IN ?", m.FieldAddAlias(fieldName)), rowAuth) } } //如果自身也是行权限模型,则进行本身数据权限过滤 if m.attr.isRowAuth { if rowAuth, isAllAuth := option.ModelAuth.GetRowAuthCallback(m.attr.Name); !isAllAuth { theDB.Where(fmt.Sprintf("%s IN ?", m.FieldAddAlias(m.attr.Pk)), rowAuth) } } return theDB } //分析查询字段 // @param extraFields 额外附加的字段 // @return fields 最终需要查询的字段名数组 // @return footFields 汇总字段 func (m *Model) ParseFields(qo *QueryOption)(fields []string,footFields []string) { fields = make([]string, 0) footFields = make([]string, 0) //扩展字段 fields = append(fields, m.FieldsAddAlias(qo.ExtraFields)...) // 树型必备字段 if m.attr.IsTree { fields = append(fields, m.ParseTreeExtraField()...) } var modelFields []*ModelField if strings.ToLower(qo.useModelFiledType) == "edit" { modelFields = m.attr.editFields }else{ modelFields = m.attr.listFields } for _, field := range modelFields { //基础字段 fieldName := "" if field.Alias == "" { fieldName = m.FieldAddAlias(field.Name) } else if field.Alias != "" { fieldName = fmt.Sprintf("%s AS %s", field.Alias, field.Name) } fields = append(fields, fieldName) //汇总字段 if field.Foot != "" { footFields = append(footFields, fmt.Sprintf("%s AS %s", field.Foot, field.Name)) } } return } // 分析kv字段数组 (仅对通过NewConfigModel创建的模型有效) // @param kvName kv配置项名 // @param extraFields 额外附加的字段 // @return fields 最终需要查询的KV字段名数组 func (m *Model) ParseKvFields(kvName string, extraFields []string) (fields []string) { fields = make([]string, 0) // kv配置中的字段 kv, ok := ModelKv{}, false if kv, ok = m.attr.Kvs[kvName]; !ok { return } //keySep := fmt.Sprintf(",'%s',", kv.KeySep) //valueSep := fmt.Sprintf(",'%s',", kv.ValueSep) keyField := fmt.Sprintf("%s AS `__mc_key`", m.FieldAddAlias(kv.KeyField)) valueField := fmt.Sprintf("%s AS `__mc_value`", m.FieldAddAlias(kv.ValueField)) fields = append(fields, keyField, valueField) // 树型必备字段 if m.attr.IsTree { treePathField := m.FieldAddAlias(m.attr.Tree.PathField) fields = append(append(fields, treePathField), m.ParseTreeExtraField()...) } // 附加字段 if extraFields != nil { fields = append(fields, m.FieldsAddAlias(extraFields)...) } return } // 给字段加表别名 func (m *Model) FieldAddAlias(field string) string { if field == "" { return "" } if strings.Contains(field, ".") || strings.Contains(field, "(") { return field } else { return fmt.Sprintf("`%s`.`%s`", m.attr.Alias, strings.Trim(field, " ")) } } // 给字段数组加表别名 func (m *Model) FieldsAddAlias(fields []string) []string { newFields := make([]string, 0) for _, v := range fields { if v == "" { continue } if strings.Contains(v, ".") || strings.Contains(v, "(") { newFields = append(newFields, v) } else { newFields = append(newFields, fmt.Sprintf("`%s`.`%s`", m.attr.Alias, strings.Trim(v, " "))) } } return newFields } // 对查询的数据进行处理 func (m *Model) ProcessData(data []map[string]interface{}, qo *QueryOption)(err error) { if data == nil || len(data) <= 0 { return } //序号 if m.attr.Number { for i, _ := range data { data[i]["__mc_index"] = (qo.Page -1) * qo.PageSize + i + 1 } } //转换成from值 if !qo.NotConvertFromValue { for _, f := range m.attr.Fields { if _, ok := data[0][f.Name]; !ok { continue } if f.From != "" { enum := m.GetFromKvs(f.From) for i, _ := range data { if qo.AttachFromRealValue { //附加字段原值真实值 data[i]["__mc_"+f.Name] = data[i][f.Name] } vString := cast.ToString(data[i][f.Name]) //字段值 if f.Multiple { //多选 vs := strings.Split(vString, f.Separator) newVs := make([]string, 0) for _, v := range vs { newVs = append(newVs, cast.ToString(enum[v]["__mc_value"])) } data[i][f.Name] = strings.Join(newVs, f.Separator) } else { //单选 data[i][f.Name] = cast.ToString(enum[vString]["__mc_value"]) } } } } } //树形 indent := "" if qo.TreeIndent == nil { indent = m.attr.Tree.Indent } else { indent = *qo.TreeIndent } if m.attr.IsTree && indent != "" { //树形名称字段加前缀 for i, _ := range data { data[i][m.attr.Tree.NameField] = nString(indent, cast.ToInt(data[i]["__mc_level"])-1) + cas
t.ToString(data[i][m.attr.Tree.NameField]) } } return } // 分析树形结构查询必须的扩展字段 func (m *Model) ParseTreeExtraField() (field []string) { pathField := m.FieldAddAlias(m.attr.Tree.PathField) __mc_pathField := fmt.Sprintf("`__mc_%s`.`%s`", m.attr.Table, m.attr.Tree.PathField) __mc_pkField := fmt.Sprintf("`__mc_%s`.`%s`
identifier_body
model_sql.go
!= nil { return } //创建数据 db := m.BaseDB(false).Create(data) return db.RowsAffected, db.Error } //保存记录(根据pk自动分析是update 或 create) func (m *Model) Save(data map[string]interface{}, oldPkValue interface{})(rowsAffected int64, err error) { if oldPkValue == nil { //创建 return m.Create(data) } else { //更新 return m.Updates(data, oldPkValue) } } //根据PK字段删除记录 func (m *Model) Delete(id interface{}) (rowsAffected int64, err error) { var delIds interface{} kind := reflect.TypeOf(id).Kind() symbol := "" if kind == reflect.Array || kind == reflect.Slice { symbol = "IN" delIds = id } else { symbol = "=" delIds = []interface{}{id} } db := m.BaseDB(false).Where(fmt.Sprintf("`%s` %s ?", m.attr.Pk, symbol), delIds).Delete(nil) return db.RowsAffected, db.Error } // 分析查询条件 (此批条件只作用于返回的db对象上,不会作用于模型的db上) // @param extraWhere 额外的查询条件 // @param searchValues 查询字段值 // @param notSearch 是否使用查询字段条件 func (m *Model) ParseWhere(db *gorm.DB, extraWhere []interface{}, searchValues map[string]interface{}, notSearch bool) *gorm.DB { var theDB *gorm.DB if db == nil { theDB = m.NewDB() } else { theDB = db.Where("") } //额外的查询条件 if extraWhere != nil { theDB.Where(extraWhere[0], extraWhere[1:]...) } // 模型各查询字段 if !notSearch { searchValues = m.ParseSearchValues(searchValues) for _, f := range m.attr.SearchFields { // 该查询字段未带条件配置 或 未传值,跳过 if _, ok := searchValues[f.Name]; !ok { continue } if f.Where == "" { f.Where = fmt.Sprintf("%s = ?", m.FieldAddAlias(f.Name)) f.Values = []string{"?"} } // 查询值与查询条件匹配 values := make([]interface{}, 0) if f.Between { //范围值 vType := reflect.TypeOf(searchValues[f.Name]).Kind() var vs []string if vType == reflect.Array || vType == reflect.Slice { vs = searchValues[f.Name].([]string) } else { vs = strings.Split(cast.ToString(searchValues[f.Name]), f.BetweenSep) } for i, v := range f.Values { if v == "?" { values = append(values, vs[i]) } else { values = append(values, strings.ReplaceAll(v, "?", vs[i])) } } } else { //单个值 for _, v := range f.Values { if v == "?" { values = append(values, searchValues[f.Name]) } else { values = append(values, strings.ReplaceAll(v, "?", cast.ToString(searchValues[f.Name]))) } } } theDB.Where(f.Where, values...) } } //受行权限控制的字段进行数据权限过滤 for fieldName, fromInfo := range m.attr.rowAuthFieldMap { if rowAuth, isAllAuth := option.ModelAuth.GetRowAuthCallback(fromInfo.FromName); !isAllAuth { theDB.Where(fmt.Sprintf("%s IN ?", m.FieldAddAlias(fieldName)), rowAuth) } } //如果自身也是行权限模型,则进行本身数据权限过滤 if m.attr.isRowAuth { if rowAuth, isAllAuth := option.ModelAuth.GetRowAuthCallback(m.attr.Name); !isAllAuth { theDB.Where(fmt.Sprintf("%s IN ?", m.FieldAddAlias(m.attr.Pk)), rowAuth) } } return theDB } //分析查询字段 // @param extraFields 额外附加的字段 // @return fields 最终需要查询的字段名数组 // @return footFields 汇总字段 func (m *Model) ParseFields(qo *QueryOption)(fields []string,footFields []string) { fields = make([]string, 0) footFields = make([]string, 0) //扩展字段 fields = append(fields, m.FieldsAddAlias(qo.ExtraFields)...) // 树型必备字段 if m.attr.IsTree { fields = append(fields, m.ParseTreeExtraField()...) } var modelFields []*ModelField if strings.ToLower(qo.useModelFiledType) == "edit" { modelFields = m.attr.editFields }else{ modelFields = m.attr.listFields } for _, field := range modelFields { //基础字段 fieldName := "" if field.Alias == "" { fieldName = m.FieldAddAlias(field.Name) } else if field.Alias != "" { fieldName = fmt.Sprintf("%s AS %s", field.Alias, field.Name) } fields = append(fields, fieldName) //汇总字段 if field.Foot != "" { footFields = append(footFields, fmt.Sprintf("%s AS %s", field.Foot, field.Name)) } } return } // 分析kv字段数组 (仅对通过NewConfigModel创建的模型有效) // @param kvName kv配置项名 // @param extraFields 额外附加的字段 // @return fields 最终需要查询的KV字段名数组 func (m *Model) ParseKvFields(kvName string, extraFields []string) (fields []string) { fields = make([]string, 0) // kv配置中的字段 kv, ok := ModelKv{}, false if kv, ok = m.attr.Kvs[kvName]; !ok { return } //keySep := fmt.Sprintf(",'%s',", kv.KeySep) //valueSep := fmt.Sprintf(",'%s',", kv.ValueSep) keyField := fmt.Sprintf("%s AS `__mc_key`", m.FieldAddAlias(kv.KeyField)) valueField := fmt.Sprintf("%s AS `__mc_value`", m.FieldAddAlias(kv.ValueField)) fields = append(fields, keyField, valueField) // 树型必备字段 if m.attr.IsTree { treePathField := m.FieldAddAlias(m.attr.Tree.PathField) fields = append(append(fields, treePathField), m.ParseTreeExtraField()...) } // 附加字段 if extraFields != nil { fields = append(fields, m.FieldsAddAlias(extraFields)...) } return } // 给字段加表别名 func (m *Model) FieldAddAlias(field string) string { if field == "" { return "" } if strings.Contains(field, ".") || strings.Contains(field, "(") { return field } else { return fmt.Sprintf("`%s`.`%s`", m.attr.Alias, strings.Trim(field, " ")) } } // 给字段数组加表别名 func (m *Model) FieldsAddAlias(fields []string) []string { newFields := make([]string, 0) for _, v := range fields { if v == "" { continue } if strings.Contains(v, ".") || strings.Contains(v, "(") { newFields = append(newFields, v) } else { newFields = append(newFields, fmt.Sprintf("`%s`.`%s`", m.attr.Alias, strings.Trim(v, " "))) } } return newFields } // 对查询的数据进行处理 func (m *Model) ProcessData(data []map[string]interface{}, qo *QueryOption)(err error) { if data == nil || len(data) <= 0 { return } //序号 if m.attr.Number { for i, _ := range data { data[i]["__mc_index"] = (qo.Page -1) * qo.PageSize + i + 1 } } //转换成from值 if !qo.NotConvertFromValue { for _, f := range m.attr.Fields { if _, ok := data[0][f.Name]; !ok { continue } if f.From != "" { enum := m.GetFromKvs(f.From) for i, _ := range data { if qo.AttachFromRealValue { //附加字段原值真实值 data[i]["__mc_"+f.Name] = data[i][f.Name] } vString := cast.ToString(data[i][f.Name]) //字段值 if f.Multiple { //多选 vs := strings.Split(vString, f.Separator) newVs := make([]string, 0) for _, v := range vs { newVs = append(newVs, cast.ToString(enum[v]["__mc_value"])) } data[i][f.Name] = strings.Join(newVs, f.Separator) } else { //单选 data[i][f.Name] = cast.ToString(enum[vStr
ing]["__mc_va
identifier_name
script.padrao.js
ext = URLHOST.host.indexOf('intru') > -1; if (ext != null && ext != undefined) { if (ext != true) { return; } } var cod = recuperaUserCodCookie(); if (cod == '' || cod == null || cod == undefined) { if (window.location.href == `${urlHost}/Security/Login/`) { return; } logOut(); } }) const Grafico = { /** * Realiza a configuração do gráfico (não faz a renderização, apenas configura, utilize o Grafico.NewChart para renderizar um grafico, passando essa função como parâmetro) * @param {string} typeChar Tipo do grafico a ser renderizado * @param {object} pdata Um objeto do tipo ChartData contendo dos dados a ser exibidos * @param {object} plabels Um objeto contendo as labels do grafico * @returns void */ ConfigChart: (typeChar, pChartData, plabels) => { try { var labels = plabels != undefined && plabels != null ? plabels : [ 'Empty' ]; var data = pChartData != undefined && pChartData != null ? pChartData : { labels: labels, datasets: [{ label: 'Dataset', backgroundColor: `rgb(${colorPrymary1Rgb})`, borderWidth: 2, borderColor: `rgb(${colorPrymary2Rgb})`, data: [0] }] }; var config = { type: typeChar != undefined && typeChar != null ? (typeChar).toString() : 'line', data, options: { title: { display: true, fontSize: 20, text: "Relatorio" } } }; return config; } catch (erro) { alert(erro); } }, /** * Essa função faz a renderização de um grafico utilizando a tag canvas * @param {*} configChart Um objeto configuravel retornado pela função Grafico.ConfigChart * @param {} canvasId Id do canvas onde o grafico sera renderizado * @returns void */ NewChart: (configChart, canvasId) => { try { if (configChart == undefined || configChart == null && canvasId == undefined || canvasId == null) { return false; } var chart = new Chart(document.getElementById(canvasId.toString()), configChart); } catch (error) { alert(error); } } } /** * Função responsável pelas requisições para a API. * @param {AjaxOptions} pOptions Objeto contendo a configuração da requisição. * @param {any} asyncRequest Um booleano indicando se a requisição é assincrona, o valor padrão é true */ const Ajax = (pOptions, asyncRequest = true) => { try { if (pOptions == undefined || pOptions == null) { return false; } if (typeof (asyncRequest) != "boolean") { return false; } var http = new XMLHttpRequest(); var options = AjaxOptions; options.onload = pOptions.onload != undefined && pOptions.onload != null ? pOptions.onload : AjaxOptions.onload; options.onerror = pOptions.onerror != undefined && pOptions.onerror != null ? pOptions.onerror : AjaxOptions.onerror; options.responseType = pOptions.responseType != undefined && pOptions.responseType != null ? pOptions.responseType : AjaxOptions.responseType; options.method = pOptions.method; options.url = pOptions.url; if (pOptions.setRequestHeader != undefined && pOptions.setRequestHeader != null) { options.setRequestHeader.name = pOptions.setRequestHeader.name != undefined && pOptions.setRequestHeader.name != null ? pOptions.setRequestHeader.name : AjaxOptions.setRequestHeader.name; options.setRequestHeader.value = pOptions.setRequestHeader.value != undefined && pOptions.setRequestHeader.value != null ? pOptions.setRequestHeader.value : AjaxOptions.setRequestHeader.value; } http.open(options.method, options.url, asyncRequest); http.responseType = options.responseType; http.setRequestHeader(options.setRequestHeader.name, options.setRequestHeader.value); http.onload = options.onload; http.onerror = options.onerror; http.onloadstart = pOptions.onloadstart != undefined && pOptions.onloadstart != null ? pOptions.onloadstart : AjaxOptions.onloadstart; http.onloadend = pOptions.onloadend != undefined && pOptions.onloadend != null ? pOptions.onloadend : AjaxOptions.onloadend; if (pOptions.data != null && pOptions.data != undefined) { var data = options.setRequestHeader.value == 'application/json' ? JSON.stringify(options.data) : encodeURI(options.data); http.send(data); } else { http.send();
alert(error); } } const API = { /** *Requisições do tipo GET * @param {Opções para a definição da requisição} options */ GET: (options) => { try { if (options == undefined || options == null) { return false; } Ajax(options); } catch (error) { alert(error); } }, /** * * @param {Opções para a definição da requisição} options */ POST: (options) => { try { Ajax(options); } catch (error) { alert(error); } } } const Elements = { /** * Retorna um elemento configurado por parametro. * @param {any} type Tipo do elemento: div, button, input, label, canvas, p, h1, etc. * @param {any} id * @param {any} name * @param {any} classe * @param {any} style * @param {any} onchange */ Create: (type = null, id = '', name = '', classe = null, style = null, classList = null, onchange = null) => { try { if (type == null && type == undefined) { return null; } var element = document.createElement(type); if (onchange != null, onchange != undefined) { element.addEventListener('change', onchange); } if (style != null && style != undefined) { element.style = style; } element.id = id; if (classList != null && Array.isArray(classList)) { for (i = 0; i < classList.length; i++) { element.classList.add(classList[i]); } } else if (classe != null) { element.classList.add(classe); } element.name = name; return element; } catch (error) { console.log(error); } }, /** * Renderiza um elemento de load na tela * @param {any} type Tipo do load que sera renderizado, o tipo padrão é "Spin" (Spin, Growing) * @param {any} idElement Id do elemento onde o load sera renderizado * @param {any} style Tipo de cor () */ Load: { Create: (type, idElement, strStyle, strSpinnerColor) => { try { var span; var div; var spinnerColor = strSpinnerColor == null || strSpinnerColor == undefined ? "var(--colorPrymary5)" : strSpinnerColor; var style = strStyle == null || strStyle == undefined ? `z-index: 999 !important; position: fixed !important; left: 50%;` + `top: 50% !important; bottom: 0 !important; color: ${spinnerColor} !important;` : strStyle; switch (type) { case "Spin": div = Elements.Create('div', 'load', null, null, style, ["spinner-border", "text-primary"]); span = Elements.Create('span', 'loadSpan', "visually-hidden", null); div.appendChild(span); break; case "Growing": div = Elements.Create('div', 'loadMestre', null, null, style); var divSpinner = Elements.Create('div', 'divGrowing', null, null, `z-index: 150 !important; color: ${spinnerColor} !important;`, ["spinner-grow", "text-primary"]); span = Elements.Create('span', 'loadGrowing', "visually-hidden", null); divSpinner.appendChild(span); div.appendChild(divSpinner); break; default: div = Elements.Create('div', 'load', null, null, style, ["spinner-border", "text-primary"]); span = Elements.Create('span', 'loadSpan', null, "visually-hidden"); div.appendChild(span); break; } if (idElement == null || idElement == undefined) { document.body.appendChild(div); } else { document.getElementById(idElement).appendChild(div); } } catch (error) { console.log(error); } }, LoadRemove: (load) => { try { if (load == null || load == undefined) { alert("O Load não pode ser nulo"); return } var ld = document.getElementById(load); if (ld != null && ld != undefined) { ld.remove(); } } catch (error) { console
} } catch (error) {
conditional_block
script.padrao.js
var ext = URLHOST.host.indexOf('intru') > -1; if (ext != null && ext != undefined) { if (ext != true) { return; } } var cod = recuperaUserCodCookie(); if (cod == '' || cod == null || cod == undefined) { if (window.location.href == `${urlHost}/Security/Login/`) { return; } logOut(); } }) const Grafico = { /** * Realiza a configuração do gráfico (não faz a renderização, apenas configura, utilize o Grafico.NewChart para renderizar um grafico, passando essa função como parâmetro) * @param {string} typeChar Tipo do grafico a ser renderizado * @param {object} pdata Um objeto do tipo ChartData contendo dos dados a ser exibidos * @param {object} plabels Um objeto contendo as labels do grafico * @returns void */ ConfigChart: (typeChar, pChartData, plabels) => { try { var labels = plabels != undefined && plabels != null ? plabels : [ 'Empty' ]; var data = pChartData != undefined && pChartData != null ? pChartData : { labels: labels, datasets: [{ label: 'Dataset', backgroundColor: `rgb(${colorPrymary1Rgb})`, borderWidth: 2, borderColor: `rgb(${colorPrymary2Rgb})`, data: [0] }] }; var config = { type: typeChar != undefined && typeChar != null ? (typeChar).toString() : 'line', data, options: { title: { display: true, fontSize: 20, text: "Relatorio" } } }; return config; } catch (erro) { alert(erro); } }, /** * Essa função faz a renderização de um grafico utilizando a tag canvas * @param {*} configChart Um objeto configuravel retornado pela função Grafico.ConfigChart * @param {} canvasId Id do canvas onde o grafico sera renderizado * @returns void */ NewChart: (configChart, canvasId) => { try { if (configChart == undefined || configChart == null && canvasId == undefined || canvasId == null) { return false; } var chart = new Chart(document.getElementById(canvasId.toString()), configChart); } catch (error) { alert(error); } } } /** * Função responsável pelas requisições para a API. * @param {AjaxOptions} pOptions Objeto contendo a configuração da requisição. * @param {any} asyncRequest Um booleano indicando se a requisição é assincrona, o valor padrão é true */ const Ajax = (pOptions, asyncRequest = true) => { try { if (pOptions == undefined || pOptions == null) { return false; } if (typeof (asyncRequest) != "boolean") { return false; } var http = new XMLHttpRequest(); var options = AjaxOptions; options.onload = pOptions.onload != undefined && pOptions.onload != null ? pOptions.onload : AjaxOptions.onload; options.onerror = pOptions.onerror != undefined && pOptions.onerror != null ? pOptions.onerror : AjaxOptions.onerror; options.responseType = pOptions.responseType != undefined && pOptions.responseType != null ? pOptions.responseType : AjaxOptions.responseType; options.method = pOptions.method; options.url = pOptions.url; if (pOptions.setRequestHeader != undefined && pOptions.setRequestHeader != null) { options.setRequestHeader.name = pOptions.setRequestHeader.name != undefined && pOptions.setRequestHeader.name != null ? pOptions.setRequestHeader.name : AjaxOptions.setRequestHeader.name; options.setRequestHeader.value = pOptions.setRequestHeader.value != undefined && pOptions.setRequestHeader.value != null ? pOptions.setRequestHeader.value : AjaxOptions.setRequestHeader.value; } http.open(options.method, options.url, asyncRequest); http.responseType = options.responseType; http.setRequestHeader(options.setRequestHeader.name, options.setRequestHeader.value); http.onload = options.onload; http.onerror = options.onerror; http.onloadstart = pOptions.onloadstart != undefined && pOptions.onloadstart != null ? pOptions.onloadstart : AjaxOptions.onloadstart; http.onloadend = pOptions.onloadend != undefined && pOptions.onloadend != null ? pOptions.onloadend : AjaxOptions.onloadend; if (pOptions.data != null && pOptions.data != undefined) { var data = options.setRequestHeader.value == 'application/json' ? JSON.stringify(options.data) : encodeURI(options.data); http.send(data); } else { http.send(); } } catch (error) { alert(error); } } const API = { /** *Requisições do tipo GET * @param {Opções para a definição da requisição} options */ GET: (options) => { try { if (options == undefined || options == null) { return false; } Ajax(options); } catch (error) { alert(error); } }, /** * * @param {Opções para a definição da requisição} options */ POST: (options) => { try { Ajax(options); } catch (error) { alert(error); } } } const Elements = { /** * Retorna um elemento configurado por parametro. * @param {any} type Tipo do elemento: div, button, input, label, canvas, p, h1, etc. * @param {any} id * @param {any} name * @param {any} classe * @param {any} style * @param {any} onchange */ Create: (type = null, id = '', name = '', classe = null, style = null, classList = null, onchange = null) => { try { if (type == null && type == undefined) { return null; } var element = document.createElement(type); if (onchange != null, onchange != undefined) { element.addEventListener('change', onchange); } if (style != null && style != undefined) { element.style = style; } element.id = id; if (classList != null && Array.isArray(classList)) { for (i = 0; i < classList.length; i++) { element.classList.add(classList[i]); } } else if (classe != null) { element.classList.add(classe); } element.name = name; return element; } catch (error) { console.log(error); } }, /** * Renderiza um elemento de load na tela * @param {any} type Tipo do load que sera renderizado, o tipo padrão é "Spin" (Spin, Growing) * @param {any} idElement Id do elemento onde o load sera renderizado * @param {any} style Tipo de cor () */ Load: { Create: (type, idElement, strStyle, strSpinnerColor) => { try { var span; var div; var spinnerColor = strSpinnerColor == null || strSpinnerColor == undefined ? "var(--colorPrymary5)" : strSpinnerColor; var style = strStyle == null || strStyle == undefined ? `z-index: 999 !important; position: fixed !important; left: 50%;` + `top: 50% !important; bottom: 0 !important; color: ${spinnerColor} !important;` : strStyle; switch (type) { case "Spin": div = Elements.Create('div', 'load', null, null, style, ["spinner-border", "text-primary"]); span = Elements.Create('span', 'loadSpan', "visually-hidden", null); div.appendChild(span); break; case "Growing":
var divSpinner = Elements.Create('div', 'divGrowing', null, null, `z-index: 150 !important; color: ${spinnerColor} !important;`, ["spinner-grow", "text-primary"]); span = Elements.Create('span', 'loadGrowing', "visually-hidden", null); divSpinner.appendChild(span); div.appendChild(divSpinner); break; default: div = Elements.Create('div', 'load', null, null, style, ["spinner-border", "text-primary"]); span = Elements.Create('span', 'loadSpan', null, "visually-hidden"); div.appendChild(span); break; } if (idElement == null || idElement == undefined) { document.body.appendChild(div); } else { document.getElementById(idElement).appendChild(div); } } catch (error) { console.log(error); } }, LoadRemove: (load) => { try { if (load == null || load == undefined) { alert("O Load não pode ser nulo"); return } var ld = document.getElementById(load); if (ld != null && ld != undefined) { ld.remove(); } } catch (error) { console.log
div = Elements.Create('div', 'loadMestre', null, null, style);
random_line_split
mod.rs
use rstd::prelude::*; use codec::{Encode, Decode}; use support::{ StorageValue, StorageMap, decl_event, decl_storage, decl_module, ensure, traits::{ Currency, ReservableCurrency, OnFreeBalanceZero, OnUnbalanced, WithdrawReason, ExistenceRequirement, Imbalance, Get, }, dispatch::Result, }; use sr_primitives::{ transaction_validity::{ TransactionPriority, ValidTransaction, InvalidTransaction, TransactionValidityError, TransactionValidity, }, traits::{ Zero, CheckedAdd, CheckedSub, Saturating, SignedExtension, SaturatedConversion, Convert, }, weights::{DispatchInfo, SimpleDispatchInfo, Weight}, }; use system::{OnNewAccount, ensure_signed}; use crate::non_transfer_asset::SustainableCurrency; /// Trait for activity pub trait ActivityInterface<AccountId, Balance> { fn admire(sender: &AccountId, target: &AccountId, cap: Balance) -> Result; } /// The module's configuration trait. pub trait Trait: system::Trait { /// Currency type for this module. type Currency: ReservableCurrency<Self::AccountId>; /// Energy type for this module type EnergyCurrency: SustainableCurrency<Self::AccountId, Moment=Self::BlockNumber>; /// Action point type for this module type ActivityCurrency: Currency<Self::AccountId>; /// Reputation point type for this module type ReputationCurrency: Currency<Self::AccountId>; /// Handler for the unbalanced reduction when taking transaction fees. type TransactionPayment: OnUnbalanced<NegativeImbalanceOf<Self>>; /// The overarching event type. type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>; /// The fee to be paid for making a transaction; the base. type TransactionBaseFee: Get<BalanceOf<Self>>; /// The fee to be paid for making a transaction; the per-byte portion. type TransactionByteFee: Get<BalanceOf<Self>>; /// The base Energy amount of activated account type EnergyBaseAmount: Get<EnergyOf<Self>>; /// Convert a weight value into a deductible fee based on the currency type. type WeightToFee: Convert<Weight, BalanceOf<Self>>; /// Convert a fee value to energy point type FeeToEnergy: Convert<BalanceOf<Self>, EnergyOf<Self>>; /// Convert a charging value to energy point type ChargingToEnergy: Convert<BalanceOf<Self>, EnergyOf<Self>>; /// Convert an energy point to fee value type EnergyToFee: Convert<EnergyOf<Self>, BalanceOf<Self>>; /// Convert an energy point to locking block number type EnergyToLocking: Convert<EnergyOf<Self>, <Self as system::Trait>::BlockNumber>; /// Convert an energy point to action point type EnergyToActionPoint: Convert<EnergyOf<Self>, ActionPointOf<Self>>; /// Convert an action point to reputation type ActionPointToReputation: Convert<ActionPointOf<Self>, ReputationOf<Self>>; } // Balance zone pub type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance; type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance; // Energy zone pub type EnergyOf<T> = <<T as Trait>::EnergyCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // Action zone pub type ActionPointOf<T> = <<T as Trait>::ActivityCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // Reputation zone pub type ReputationOf<T> = <<T as Trait>::ReputationCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // This module's storage items. decl_storage! { trait Store for Module<T: Trait> as Activities { /// Map from all extend pub Charged get(charged): map T::AccountId => BalanceOf<T>; } } decl_event!( pub enum Event<T> where AccountId = <T as system::Trait>::AccountId, Balance = BalanceOf<T>, Energy = EnergyOf<T>, Reputation = ReputationOf<T> { // Fee payment FeePayed(AccountId, Energy, Balance), EnergyRecovered(AccountId, Energy), // Reputation part ReputationReward(AccountId, Reputation), ReputationSlash(AccountId, Reputation), } ); // The module's dispatchable functions. decl_module! { /// The module declaration. pub struct Module<T: Trait> for enum Call where origin: T::Origin { // Initializing events fn deposit_event() = default; /// Bond to increase Energy #[weight = SimpleDispatchInfo::FixedNormal(1_000_000)] pub fn charge( origin, #[compact] value: BalanceOf<T> ) { let who = ensure_signed(origin)?; Self::charge_for_energy(&who, value)?; } /// UnBond to decrease Energy #[weight = SimpleDispatchInfo::FixedNormal(1_000_000)] pub fn discharge( origin, #[compact] value: BalanceOf<T> ) { let who = ensure_signed(origin)?; Self::discharge_for_energy(&who, value)?; } } } // The module's main implement impl<T: Trait> Module<T> { // PUBLIC IMMUTABLES pub fn available_energy(who: &T::AccountId) -> EnergyOf<T> { T::EnergyCurrency::available_free_balance(who) } // PRIVATE MUTABLES fn charge_for_energy(who: &T::AccountId, value: BalanceOf<T>) -> Result { // ensure reserve if !T::Currency::can_reserve(who, value) { return Err("not enough free funds"); } // check current_charged let current_charged = <Charged<T>>::get(who); let new_charged = current_charged.checked_add(&value).ok_or("account has charged overflow")?; let energy_to_charge = T::ChargingToEnergy::convert(value); let current_energy = T::EnergyCurrency::free_balance(who); current_energy.checked_add(&energy_to_charge).ok_or("Overflow energy amount")?; // MUTABLES T::Currency::reserve(who, value)?; T::EnergyCurrency::deposit_into_existing(who, energy_to_charge)?; <Charged<T>>::insert(who, new_charged); Ok(()) } fn discharge_for_energy(who: &T::AccountId, value: BalanceOf<T>) -> Result { // check current_charged let current_charged = <Charged<T>>::get(who); let new_charged = current_charged.checked_sub(&value).ok_or("account has too few charged funds")?; let energy_to_discharge = T::ChargingToEnergy::convert(value); let current_energy = T::EnergyCurrency::free_balance(who); current_energy.checked_sub(&energy_to_discharge).ok_or("account has too few energy")?; // MUTABLES T::EnergyCurrency::withdraw(who, energy_to_discharge, WithdrawReason::Fee, ExistenceRequirement::KeepAlive)?; T::Currency::unreserve(who, value); <Charged<T>>::insert(who, new_charged); Ok(()) } } impl<T: Trait> OnNewAccount<T::AccountId> for Module<T> { // Implementation of the config type managing the creation of new accounts. fn on_new_account(who: &T::AccountId) { T::EnergyCurrency::deposit_creating(who, T::EnergyBaseAmount::get()); } } impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> { fn on_free_balance_zero(who: &T::AccountId) { let dust = <Charged<T>>::take(who); if !dust.is_zero() { T::Currency::unreserve(who, dust); } T::EnergyCurrency::slash(who, T::EnergyCurrency::total_balance(who)); } } impl<T: Trait> ActivityInterface<T::AccountId, ActionPointOf<T>> for Module<T> { // do admire fn admire(sender: &T::AccountId, target: &T::AccountId, cap: ActionPointOf<T>) -> Result { let earned_rp = T::ActionPointToReputation::convert(cap.clone()); ensure!(!earned_rp.is_zero(), "action point too low "); T::ActivityCurrency::withdraw(sender, cap, WithdrawReason::Fee, ExistenceRequirement::KeepAlive)?; T::ReputationCurrency::deposit_into_existing(target, earned_rp).unwrap(); Ok(()) } } /// Require the transactor pay for themselves and maybe include a tip to gain additional priority /// in the queue. #[derive(Encode, Decode, Clone, Eq, PartialEq)] pub struct TakeFees<T: Trait>(#[codec(compact)] BalanceOf<T>); impl<T: Trait> TakeFees<T> { /// utility constructor. Used only in client/factory code. pub fn from(fee: BalanceOf<T>) -> Self { Self(fee) } /// Compute the final fee value for a particular transaction. /// /// The final fee is composed of: ///
//! # Activity Module //! #![cfg_attr(not(feature = "std"), no_std)]
random_line_split
mod.rs
this module type ReputationCurrency: Currency<Self::AccountId>; /// Handler for the unbalanced reduction when taking transaction fees. type TransactionPayment: OnUnbalanced<NegativeImbalanceOf<Self>>; /// The overarching event type. type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>; /// The fee to be paid for making a transaction; the base. type TransactionBaseFee: Get<BalanceOf<Self>>; /// The fee to be paid for making a transaction; the per-byte portion. type TransactionByteFee: Get<BalanceOf<Self>>; /// The base Energy amount of activated account type EnergyBaseAmount: Get<EnergyOf<Self>>; /// Convert a weight value into a deductible fee based on the currency type. type WeightToFee: Convert<Weight, BalanceOf<Self>>; /// Convert a fee value to energy point type FeeToEnergy: Convert<BalanceOf<Self>, EnergyOf<Self>>; /// Convert a charging value to energy point type ChargingToEnergy: Convert<BalanceOf<Self>, EnergyOf<Self>>; /// Convert an energy point to fee value type EnergyToFee: Convert<EnergyOf<Self>, BalanceOf<Self>>; /// Convert an energy point to locking block number type EnergyToLocking: Convert<EnergyOf<Self>, <Self as system::Trait>::BlockNumber>; /// Convert an energy point to action point type EnergyToActionPoint: Convert<EnergyOf<Self>, ActionPointOf<Self>>; /// Convert an action point to reputation type ActionPointToReputation: Convert<ActionPointOf<Self>, ReputationOf<Self>>; } // Balance zone pub type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance; type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance; // Energy zone pub type EnergyOf<T> = <<T as Trait>::EnergyCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // Action zone pub type ActionPointOf<T> = <<T as Trait>::ActivityCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // Reputation zone pub type ReputationOf<T> = <<T as Trait>::ReputationCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // This module's storage items. decl_storage! { trait Store for Module<T: Trait> as Activities { /// Map from all extend pub Charged get(charged): map T::AccountId => BalanceOf<T>; } } decl_event!( pub enum Event<T> where AccountId = <T as system::Trait>::AccountId, Balance = BalanceOf<T>, Energy = EnergyOf<T>, Reputation = ReputationOf<T> { // Fee payment FeePayed(AccountId, Energy, Balance), EnergyRecovered(AccountId, Energy), // Reputation part ReputationReward(AccountId, Reputation), ReputationSlash(AccountId, Reputation), } ); // The module's dispatchable functions. decl_module! { /// The module declaration. pub struct Module<T: Trait> for enum Call where origin: T::Origin { // Initializing events fn deposit_event() = default; /// Bond to increase Energy #[weight = SimpleDispatchInfo::FixedNormal(1_000_000)] pub fn charge( origin, #[compact] value: BalanceOf<T> ) { let who = ensure_signed(origin)?; Self::charge_for_energy(&who, value)?; } /// UnBond to decrease Energy #[weight = SimpleDispatchInfo::FixedNormal(1_000_000)] pub fn discharge( origin, #[compact] value: BalanceOf<T> ) { let who = ensure_signed(origin)?; Self::discharge_for_energy(&who, value)?; } } } // The module's main implement impl<T: Trait> Module<T> { // PUBLIC IMMUTABLES pub fn available_energy(who: &T::AccountId) -> EnergyOf<T> { T::EnergyCurrency::available_free_balance(who) } // PRIVATE MUTABLES fn charge_for_energy(who: &T::AccountId, value: BalanceOf<T>) -> Result { // ensure reserve if !T::Currency::can_reserve(who, value) { return Err("not enough free funds"); } // check current_charged let current_charged = <Charged<T>>::get(who); let new_charged = current_charged.checked_add(&value).ok_or("account has charged overflow")?; let energy_to_charge = T::ChargingToEnergy::convert(value); let current_energy = T::EnergyCurrency::free_balance(who); current_energy.checked_add(&energy_to_charge).ok_or("Overflow energy amount")?; // MUTABLES T::Currency::reserve(who, value)?; T::EnergyCurrency::deposit_into_existing(who, energy_to_charge)?; <Charged<T>>::insert(who, new_charged); Ok(()) } fn discharge_for_energy(who: &T::AccountId, value: BalanceOf<T>) -> Result { // check current_charged let current_charged = <Charged<T>>::get(who); let new_charged = current_charged.checked_sub(&value).ok_or("account has too few charged funds")?; let energy_to_discharge = T::ChargingToEnergy::convert(value); let current_energy = T::EnergyCurrency::free_balance(who); current_energy.checked_sub(&energy_to_discharge).ok_or("account has too few energy")?; // MUTABLES T::EnergyCurrency::withdraw(who, energy_to_discharge, WithdrawReason::Fee, ExistenceRequirement::KeepAlive)?; T::Currency::unreserve(who, value); <Charged<T>>::insert(who, new_charged); Ok(()) } } impl<T: Trait> OnNewAccount<T::AccountId> for Module<T> { // Implementation of the config type managing the creation of new accounts. fn on_new_account(who: &T::AccountId) { T::EnergyCurrency::deposit_creating(who, T::EnergyBaseAmount::get()); } } impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> { fn on_free_balance_zero(who: &T::AccountId) { let dust = <Charged<T>>::take(who); if !dust.is_zero() { T::Currency::unreserve(who, dust); } T::EnergyCurrency::slash(who, T::EnergyCurrency::total_balance(who)); } } impl<T: Trait> ActivityInterface<T::AccountId, ActionPointOf<T>> for Module<T> { // do admire fn admire(sender: &T::AccountId, target: &T::AccountId, cap: ActionPointOf<T>) -> Result { let earned_rp = T::ActionPointToReputation::convert(cap.clone()); ensure!(!earned_rp.is_zero(), "action point too low "); T::ActivityCurrency::withdraw(sender, cap, WithdrawReason::Fee, ExistenceRequirement::KeepAlive)?; T::ReputationCurrency::deposit_into_existing(target, earned_rp).unwrap(); Ok(()) } } /// Require the transactor pay for themselves and maybe include a tip to gain additional priority /// in the queue. #[derive(Encode, Decode, Clone, Eq, PartialEq)] pub struct
<T: Trait>(#[codec(compact)] BalanceOf<T>); impl<T: Trait> TakeFees<T> { /// utility constructor. Used only in client/factory code. pub fn from(fee: BalanceOf<T>) -> Self { Self(fee) } /// Compute the final fee value for a particular transaction. /// /// The final fee is composed of: /// - _length-fee_: This is the amount paid merely to pay for size of the transaction. /// - _weight-fee_: This amount is computed based on the weight of the transaction. Unlike /// size-fee, this is not input dependent and reflects the _complexity_ of the execution /// and the time it consumes. /// - (optional) _tip_: if included in the transaction, it will be added on top. Only signed /// transactions can have a tip. fn compute_fee(len: usize, info: DispatchInfo, tip: BalanceOf<T>) -> BalanceOf<T> { let len_fee = if info.pay_length_fee() { let len = <BalanceOf<T> as From<u32>>::from(len as u32); let base = T::TransactionBaseFee::get(); let per_byte = T::TransactionByteFee::get(); base.saturating_add(per_byte.saturating_mul(len)) } else { Zero::zero() }; let weight_fee = { // cap the weight to the maximum defined in runtime, otherwise it will be the `Bounded` // maximum of its data type, which is not desired. let capped_weight = info.weight.min(<T as system::Trait>::MaximumBlockWeight::get()); let weight_update = <system::Module<T>>::next_weight_multiplier(); let adjusted_weight = weight_update.apply_to(capped_weight
TakeFees
identifier_name
mod.rs
this module type ReputationCurrency: Currency<Self::AccountId>; /// Handler for the unbalanced reduction when taking transaction fees. type TransactionPayment: OnUnbalanced<NegativeImbalanceOf<Self>>; /// The overarching event type. type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>; /// The fee to be paid for making a transaction; the base. type TransactionBaseFee: Get<BalanceOf<Self>>; /// The fee to be paid for making a transaction; the per-byte portion. type TransactionByteFee: Get<BalanceOf<Self>>; /// The base Energy amount of activated account type EnergyBaseAmount: Get<EnergyOf<Self>>; /// Convert a weight value into a deductible fee based on the currency type. type WeightToFee: Convert<Weight, BalanceOf<Self>>; /// Convert a fee value to energy point type FeeToEnergy: Convert<BalanceOf<Self>, EnergyOf<Self>>; /// Convert a charging value to energy point type ChargingToEnergy: Convert<BalanceOf<Self>, EnergyOf<Self>>; /// Convert an energy point to fee value type EnergyToFee: Convert<EnergyOf<Self>, BalanceOf<Self>>; /// Convert an energy point to locking block number type EnergyToLocking: Convert<EnergyOf<Self>, <Self as system::Trait>::BlockNumber>; /// Convert an energy point to action point type EnergyToActionPoint: Convert<EnergyOf<Self>, ActionPointOf<Self>>; /// Convert an action point to reputation type ActionPointToReputation: Convert<ActionPointOf<Self>, ReputationOf<Self>>; } // Balance zone pub type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance; type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance; // Energy zone pub type EnergyOf<T> = <<T as Trait>::EnergyCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // Action zone pub type ActionPointOf<T> = <<T as Trait>::ActivityCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // Reputation zone pub type ReputationOf<T> = <<T as Trait>::ReputationCurrency as Currency<<T as system::Trait>::AccountId>>::Balance; // This module's storage items. decl_storage! { trait Store for Module<T: Trait> as Activities { /// Map from all extend pub Charged get(charged): map T::AccountId => BalanceOf<T>; } } decl_event!( pub enum Event<T> where AccountId = <T as system::Trait>::AccountId, Balance = BalanceOf<T>, Energy = EnergyOf<T>, Reputation = ReputationOf<T> { // Fee payment FeePayed(AccountId, Energy, Balance), EnergyRecovered(AccountId, Energy), // Reputation part ReputationReward(AccountId, Reputation), ReputationSlash(AccountId, Reputation), } ); // The module's dispatchable functions. decl_module! { /// The module declaration. pub struct Module<T: Trait> for enum Call where origin: T::Origin { // Initializing events fn deposit_event() = default; /// Bond to increase Energy #[weight = SimpleDispatchInfo::FixedNormal(1_000_000)] pub fn charge( origin, #[compact] value: BalanceOf<T> ) { let who = ensure_signed(origin)?; Self::charge_for_energy(&who, value)?; } /// UnBond to decrease Energy #[weight = SimpleDispatchInfo::FixedNormal(1_000_000)] pub fn discharge( origin, #[compact] value: BalanceOf<T> ) { let who = ensure_signed(origin)?; Self::discharge_for_energy(&who, value)?; } } } // The module's main implement impl<T: Trait> Module<T> { // PUBLIC IMMUTABLES pub fn available_energy(who: &T::AccountId) -> EnergyOf<T>
// PRIVATE MUTABLES fn charge_for_energy(who: &T::AccountId, value: BalanceOf<T>) -> Result { // ensure reserve if !T::Currency::can_reserve(who, value) { return Err("not enough free funds"); } // check current_charged let current_charged = <Charged<T>>::get(who); let new_charged = current_charged.checked_add(&value).ok_or("account has charged overflow")?; let energy_to_charge = T::ChargingToEnergy::convert(value); let current_energy = T::EnergyCurrency::free_balance(who); current_energy.checked_add(&energy_to_charge).ok_or("Overflow energy amount")?; // MUTABLES T::Currency::reserve(who, value)?; T::EnergyCurrency::deposit_into_existing(who, energy_to_charge)?; <Charged<T>>::insert(who, new_charged); Ok(()) } fn discharge_for_energy(who: &T::AccountId, value: BalanceOf<T>) -> Result { // check current_charged let current_charged = <Charged<T>>::get(who); let new_charged = current_charged.checked_sub(&value).ok_or("account has too few charged funds")?; let energy_to_discharge = T::ChargingToEnergy::convert(value); let current_energy = T::EnergyCurrency::free_balance(who); current_energy.checked_sub(&energy_to_discharge).ok_or("account has too few energy")?; // MUTABLES T::EnergyCurrency::withdraw(who, energy_to_discharge, WithdrawReason::Fee, ExistenceRequirement::KeepAlive)?; T::Currency::unreserve(who, value); <Charged<T>>::insert(who, new_charged); Ok(()) } } impl<T: Trait> OnNewAccount<T::AccountId> for Module<T> { // Implementation of the config type managing the creation of new accounts. fn on_new_account(who: &T::AccountId) { T::EnergyCurrency::deposit_creating(who, T::EnergyBaseAmount::get()); } } impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> { fn on_free_balance_zero(who: &T::AccountId) { let dust = <Charged<T>>::take(who); if !dust.is_zero() { T::Currency::unreserve(who, dust); } T::EnergyCurrency::slash(who, T::EnergyCurrency::total_balance(who)); } } impl<T: Trait> ActivityInterface<T::AccountId, ActionPointOf<T>> for Module<T> { // do admire fn admire(sender: &T::AccountId, target: &T::AccountId, cap: ActionPointOf<T>) -> Result { let earned_rp = T::ActionPointToReputation::convert(cap.clone()); ensure!(!earned_rp.is_zero(), "action point too low "); T::ActivityCurrency::withdraw(sender, cap, WithdrawReason::Fee, ExistenceRequirement::KeepAlive)?; T::ReputationCurrency::deposit_into_existing(target, earned_rp).unwrap(); Ok(()) } } /// Require the transactor pay for themselves and maybe include a tip to gain additional priority /// in the queue. #[derive(Encode, Decode, Clone, Eq, PartialEq)] pub struct TakeFees<T: Trait>(#[codec(compact)] BalanceOf<T>); impl<T: Trait> TakeFees<T> { /// utility constructor. Used only in client/factory code. pub fn from(fee: BalanceOf<T>) -> Self { Self(fee) } /// Compute the final fee value for a particular transaction. /// /// The final fee is composed of: /// - _length-fee_: This is the amount paid merely to pay for size of the transaction. /// - _weight-fee_: This amount is computed based on the weight of the transaction. Unlike /// size-fee, this is not input dependent and reflects the _complexity_ of the execution /// and the time it consumes. /// - (optional) _tip_: if included in the transaction, it will be added on top. Only signed /// transactions can have a tip. fn compute_fee(len: usize, info: DispatchInfo, tip: BalanceOf<T>) -> BalanceOf<T> { let len_fee = if info.pay_length_fee() { let len = <BalanceOf<T> as From<u32>>::from(len as u32); let base = T::TransactionBaseFee::get(); let per_byte = T::TransactionByteFee::get(); base.saturating_add(per_byte.saturating_mul(len)) } else { Zero::zero() }; let weight_fee = { // cap the weight to the maximum defined in runtime, otherwise it will be the `Bounded` // maximum of its data type, which is not desired. let capped_weight = info.weight.min(<T as system::Trait>::MaximumBlockWeight::get()); let weight_update = <system::Module<T>>::next_weight_multiplier(); let adjusted_weight = weight_update.apply_to(capped
{ T::EnergyCurrency::available_free_balance(who) }
identifier_body
compile.ts
_INSIDE_PATH_BINARY } from "./sandbox"; import config, { serverSideConfig } from "./config"; import { runTaskQueued } from "./taskQueue"; import { getFile, getFileHash } from "./file"; import * as fsNative from "./fsNative"; export interface CompilationConfig extends SandboxConfigWithoutMountInfo { messageFile?: string; // The file contains the message to display for user (in the binary directory) extraInfoFile?: string; // The file contains the extra information for running the compiled program (in the binary directory) workingDirectory: string; // The working directory for the compiler or script } export interface CompileTask { language: string; code: string; compileAndRunOptions: unknown; extraSourceFiles?: Record<string, string>; } async function hashCompileTask(compileTask: CompileTask): Promise<string> { return objectHash({ language: compileTask.language, code: compileTask.code, compileAndRunOptions: compileTask.compileAndRunOptions, extraSourceFiles: compileTask.extraSourceFiles && (await Promise.all( Object.entries(compileTask.extraSourceFiles).map(async ([filename, fileUuid]) => [ filename, await getFileHash(fileUuid) ]) )) }); } export interface CompileResult { compileTaskHash: string; success: boolean; message: OmittableString; } // These class implements reference count to prevent a compile result being deleted // from the disk during using export class CompileResultSuccess implements CompileResult { public readonly success: true = true; constructor( public readonly compileTaskHash: string, public readonly message: OmittableString, public readonly binaryDirectory: string, public readonly binaryDirectorySize: number, public readonly extraInfo: string ) {} // The referenceCount is initially zero, the result must be referenced at least once // Then when dereferenced to zero it will be deleted from the disk private referenceCount: number = 0; public reference()
public async dereference() { if (--this.referenceCount === 0) { await fsNative.remove(this.binaryDirectory); } } async copyTo(newBinaryDirectory: string) { this.reference(); await fsNative.copy(this.binaryDirectory, newBinaryDirectory); await this.dereference(); return new CompileResultSuccess( this.compileTaskHash, this.message, newBinaryDirectory, this.binaryDirectorySize, this.extraInfo ); } } // Why NOT using the task hash as the directory name? Because there'll be a race condition // If a compile result is disposed from the cache, but still have at least one reference // e.g. referenced by a judge task which have not finished copying the binary files to its working directory // Another cache set operation with the same task will overwrite the files (and may cause the judge task using a corrupted file) // Use a random uuid as the key instead to prevent this class CompileResultCache { private readonly lruCache = new LruCache<string, CompileResultSuccess>({ max: config.binaryCacheMaxSize, length: result => result.binaryDirectorySize, dispose: (compileTaskHash, result) => { winston.verbose(`dispose() from compile result cache: ${compileTaskHash}`); setImmediate(() => { // It's safe NOT to await it.. result.dereference().catch(e => winston.error(`Failed to remove compile result on evicting cache: ${e.stack}`)); }); } }); // The set()/get()'s returned result is reference()-ed // and must be dereference()-ed public get(compileTaskHash: string): CompileResultSuccess { if (this.lruCache.has(compileTaskHash)) return this.lruCache.get(compileTaskHash).reference(); return null; } // set() should not be called twice with the same compileTaskHash in the same time // i.e. call another time with the same compileTaskHash before the previous finished public async set(compileTaskHash: string, result: CompileResultSuccess): Promise<CompileResultSuccess> { if (this.lruCache.has(compileTaskHash)) return this.lruCache.get(compileTaskHash).reference(); const newCompileResult = await result.copyTo(safelyJoinPath(config.binaryCacheStore, uuid())); newCompileResult.reference(); this.lruCache.set(compileTaskHash, newCompileResult); return newCompileResult.reference(); } } interface PendingCompileTask { resultConsumers: ((compileResult: CompileResult) => void)[]; promise: Promise<void>; } // If there're multiple calls to compile() with the same compileTask, it's to prevent the task to be compiled multiple times // compileTaskHash -> Promise of task const pendingCompileTasks: Map<string, PendingCompileTask> = new Map(); const compileResultCache = new CompileResultCache(); export async function compile(compileTask: CompileTask): Promise<CompileResult> { const languageConfig = getLanguage(compileTask.language); const compileTaskHash = await hashCompileTask(compileTask); const cachedResult = compileResultCache.get(compileTaskHash); if (cachedResult) { winston.verbose(`Use cached compile reslt for ${compileTaskHash}`); return cachedResult; } let pendingCompileTask = pendingCompileTasks.get(compileTaskHash); if (!pendingCompileTask) { // Use a array of functions to ensure every calls to compile() of this task could get // a valid CompileResultSuccess object (with positive referenceCount) // I don't think "await promise" is guaranteed to return in a synchronous flow after the promise resolved const resultConsumers = []; pendingCompileTasks.set( compileTaskHash, (pendingCompileTask = { resultConsumers, promise: runTaskQueued(async taskWorkingDirectory => { // The compileResult is already reference()-ed const compileResult = await doCompile(compileTask, compileTaskHash, languageConfig, taskWorkingDirectory); winston.verbose(`Compile result: ${JSON.stringify(compileResult)}`); for (const resultConsumer of resultConsumers) resultConsumer(compileResult instanceof CompileResultSuccess ? compileResult.reference() : compileResult); if (compileResult instanceof CompileResultSuccess) await compileResult.dereference(); }).finally(() => pendingCompileTasks.delete(compileTaskHash)) }) ); } let result: CompileResult; pendingCompileTask.resultConsumers.push(r => { result = r; }); await pendingCompileTask.promise; return result; } // Return reference()-ed result if success async function doCompile( compileTask: CompileTask, compileTaskHash: string, languageConfig: LanguageConfig<unknown>, taskWorkingDirectory: string ): Promise<CompileResult> { const { sourceFilename, binarySizeLimit } = languageConfig.getMetaOptions(compileTask.compileAndRunOptions); const sourceDirectory: MappedPath = { outside: safelyJoinPath(taskWorkingDirectory, "source"), inside: SANDBOX_INSIDE_PATH_SOURCE }; const binaryDirectory: MappedPath = { outside: safelyJoinPath(taskWorkingDirectory, "working"), inside: SANDBOX_INSIDE_PATH_BINARY }; const tempDirectoryOutside = safelyJoinPath(taskWorkingDirectory, "temp"); await Promise.all([ ensureDirectoryEmpty(sourceDirectory.outside), ensureDirectoryEmpty(binaryDirectory.outside), ensureDirectoryEmpty(tempDirectoryOutside) ]); await Promise.all( Object.entries(compileTask.extraSourceFiles || {}).map(([dst, src]) => fs.promises.copyFile(getFile(src), safelyJoinPath(sourceDirectory.outside, dst)) ) ); const sourceFile = safelyJoinPath(sourceDirectory, sourceFilename); await fs.promises.writeFile(sourceFile.outside, compileTask.code); const compileConfig = languageConfig.compile({ sourceDirectoryInside: sourceDirectory.inside, sourcePathInside: sourceFile.inside, binaryDirectoryInside: binaryDirectory.inside, compileAndRunOptions: compileTask.compileAndRunOptions }); // The `taskId` parameter of `runSandbox` is just used to cancel the sandbox // But compilation couldn't be cancelled since multiple submissions may share the same compilation const sandboxResult = await runSandbox(null, { ...compileConfig, tempDirectoryOutside, extraMounts: [ { mappedPath: sourceDirectory, readOnly: true }, { mappedPath: binaryDirectory, readOnly: false } ] }); const messageFile = safelyJoinPath(binaryDirectory, compileConfig.messageFile); const extraInfoFile = compileConfig.extraInfoFile && safelyJoinPath(binaryDirectory, compileConfig.extraInfoFile); const [message, extraInfo] = await Promise.all([ readFileOmitted(messageFile.outside, serverSideConfig.limit.compilerMessage).then(result => result || ""), extraInfoFile ? fsNative .exists(extraInfoFile.outside) .then(exists => (exists ? fs.promises.readFile(extraInfoFile.outside, "utf-8") : null)) : null ]); await Promise.all([ fsNative.remove(messageFile.outside), extraInfoFile ? fsNative.remove(extraInfoFile.outside) : null ]); if (sandboxResult.status === SandboxStatus.OK) { if (sandboxResult.code === 0) {
{ this.referenceCount++; return this; }
identifier_body
compile.ts
OX_INSIDE_PATH_BINARY } from "./sandbox"; import config, { serverSideConfig } from "./config"; import { runTaskQueued } from "./taskQueue"; import { getFile, getFileHash } from "./file"; import * as fsNative from "./fsNative"; export interface CompilationConfig extends SandboxConfigWithoutMountInfo { messageFile?: string; // The file contains the message to display for user (in the binary directory) extraInfoFile?: string; // The file contains the extra information for running the compiled program (in the binary directory) workingDirectory: string; // The working directory for the compiler or script } export interface CompileTask { language: string; code: string; compileAndRunOptions: unknown; extraSourceFiles?: Record<string, string>; } async function hashCompileTask(compileTask: CompileTask): Promise<string> { return objectHash({ language: compileTask.language, code: compileTask.code, compileAndRunOptions: compileTask.compileAndRunOptions, extraSourceFiles: compileTask.extraSourceFiles && (await Promise.all( Object.entries(compileTask.extraSourceFiles).map(async ([filename, fileUuid]) => [ filename, await getFileHash(fileUuid) ]) )) }); } export interface CompileResult { compileTaskHash: string; success: boolean; message: OmittableString; } // These class implements reference count to prevent a compile result being deleted // from the disk during using export class CompileResultSuccess implements CompileResult { public readonly success: true = true; constructor( public readonly compileTaskHash: string, public readonly message: OmittableString, public readonly binaryDirectory: string, public readonly binaryDirectorySize: number, public readonly extraInfo: string ) {} // The referenceCount is initially zero, the result must be referenced at least once // Then when dereferenced to zero it will be deleted from the disk private referenceCount: number = 0; public reference() { this.referenceCount++; return this; } public async
() { if (--this.referenceCount === 0) { await fsNative.remove(this.binaryDirectory); } } async copyTo(newBinaryDirectory: string) { this.reference(); await fsNative.copy(this.binaryDirectory, newBinaryDirectory); await this.dereference(); return new CompileResultSuccess( this.compileTaskHash, this.message, newBinaryDirectory, this.binaryDirectorySize, this.extraInfo ); } } // Why NOT using the task hash as the directory name? Because there'll be a race condition // If a compile result is disposed from the cache, but still have at least one reference // e.g. referenced by a judge task which have not finished copying the binary files to its working directory // Another cache set operation with the same task will overwrite the files (and may cause the judge task using a corrupted file) // Use a random uuid as the key instead to prevent this class CompileResultCache { private readonly lruCache = new LruCache<string, CompileResultSuccess>({ max: config.binaryCacheMaxSize, length: result => result.binaryDirectorySize, dispose: (compileTaskHash, result) => { winston.verbose(`dispose() from compile result cache: ${compileTaskHash}`); setImmediate(() => { // It's safe NOT to await it.. result.dereference().catch(e => winston.error(`Failed to remove compile result on evicting cache: ${e.stack}`)); }); } }); // The set()/get()'s returned result is reference()-ed // and must be dereference()-ed public get(compileTaskHash: string): CompileResultSuccess { if (this.lruCache.has(compileTaskHash)) return this.lruCache.get(compileTaskHash).reference(); return null; } // set() should not be called twice with the same compileTaskHash in the same time // i.e. call another time with the same compileTaskHash before the previous finished public async set(compileTaskHash: string, result: CompileResultSuccess): Promise<CompileResultSuccess> { if (this.lruCache.has(compileTaskHash)) return this.lruCache.get(compileTaskHash).reference(); const newCompileResult = await result.copyTo(safelyJoinPath(config.binaryCacheStore, uuid())); newCompileResult.reference(); this.lruCache.set(compileTaskHash, newCompileResult); return newCompileResult.reference(); } } interface PendingCompileTask { resultConsumers: ((compileResult: CompileResult) => void)[]; promise: Promise<void>; } // If there're multiple calls to compile() with the same compileTask, it's to prevent the task to be compiled multiple times // compileTaskHash -> Promise of task const pendingCompileTasks: Map<string, PendingCompileTask> = new Map(); const compileResultCache = new CompileResultCache(); export async function compile(compileTask: CompileTask): Promise<CompileResult> { const languageConfig = getLanguage(compileTask.language); const compileTaskHash = await hashCompileTask(compileTask); const cachedResult = compileResultCache.get(compileTaskHash); if (cachedResult) { winston.verbose(`Use cached compile reslt for ${compileTaskHash}`); return cachedResult; } let pendingCompileTask = pendingCompileTasks.get(compileTaskHash); if (!pendingCompileTask) { // Use a array of functions to ensure every calls to compile() of this task could get // a valid CompileResultSuccess object (with positive referenceCount) // I don't think "await promise" is guaranteed to return in a synchronous flow after the promise resolved const resultConsumers = []; pendingCompileTasks.set( compileTaskHash, (pendingCompileTask = { resultConsumers, promise: runTaskQueued(async taskWorkingDirectory => { // The compileResult is already reference()-ed const compileResult = await doCompile(compileTask, compileTaskHash, languageConfig, taskWorkingDirectory); winston.verbose(`Compile result: ${JSON.stringify(compileResult)}`); for (const resultConsumer of resultConsumers) resultConsumer(compileResult instanceof CompileResultSuccess ? compileResult.reference() : compileResult); if (compileResult instanceof CompileResultSuccess) await compileResult.dereference(); }).finally(() => pendingCompileTasks.delete(compileTaskHash)) }) ); } let result: CompileResult; pendingCompileTask.resultConsumers.push(r => { result = r; }); await pendingCompileTask.promise; return result; } // Return reference()-ed result if success async function doCompile( compileTask: CompileTask, compileTaskHash: string, languageConfig: LanguageConfig<unknown>, taskWorkingDirectory: string ): Promise<CompileResult> { const { sourceFilename, binarySizeLimit } = languageConfig.getMetaOptions(compileTask.compileAndRunOptions); const sourceDirectory: MappedPath = { outside: safelyJoinPath(taskWorkingDirectory, "source"), inside: SANDBOX_INSIDE_PATH_SOURCE }; const binaryDirectory: MappedPath = { outside: safelyJoinPath(taskWorkingDirectory, "working"), inside: SANDBOX_INSIDE_PATH_BINARY }; const tempDirectoryOutside = safelyJoinPath(taskWorkingDirectory, "temp"); await Promise.all([ ensureDirectoryEmpty(sourceDirectory.outside), ensureDirectoryEmpty(binaryDirectory.outside), ensureDirectoryEmpty(tempDirectoryOutside) ]); await Promise.all( Object.entries(compileTask.extraSourceFiles || {}).map(([dst, src]) => fs.promises.copyFile(getFile(src), safelyJoinPath(sourceDirectory.outside, dst)) ) ); const sourceFile = safelyJoinPath(sourceDirectory, sourceFilename); await fs.promises.writeFile(sourceFile.outside, compileTask.code); const compileConfig = languageConfig.compile({ sourceDirectoryInside: sourceDirectory.inside, sourcePathInside: sourceFile.inside, binaryDirectoryInside: binaryDirectory.inside, compileAndRunOptions: compileTask.compileAndRunOptions }); // The `taskId` parameter of `runSandbox` is just used to cancel the sandbox // But compilation couldn't be cancelled since multiple submissions may share the same compilation const sandboxResult = await runSandbox(null, { ...compileConfig, tempDirectoryOutside, extraMounts: [ { mappedPath: sourceDirectory, readOnly: true }, { mappedPath: binaryDirectory, readOnly: false } ] }); const messageFile = safelyJoinPath(binaryDirectory, compileConfig.messageFile); const extraInfoFile = compileConfig.extraInfoFile && safelyJoinPath(binaryDirectory, compileConfig.extraInfoFile); const [message, extraInfo] = await Promise.all([ readFileOmitted(messageFile.outside, serverSideConfig.limit.compilerMessage).then(result => result || ""), extraInfoFile ? fsNative .exists(extraInfoFile.outside) .then(exists => (exists ? fs.promises.readFile(extraInfoFile.outside, "utf-8") : null)) : null ]); await Promise.all([ fsNative.remove(messageFile.outside), extraInfoFile ? fsNative.remove(extraInfoFile.outside) : null ]); if (sandboxResult.status === SandboxStatus.OK) { if (sandboxResult.code === 0) {
dereference
identifier_name
compile.ts
OX_INSIDE_PATH_BINARY } from "./sandbox"; import config, { serverSideConfig } from "./config"; import { runTaskQueued } from "./taskQueue"; import { getFile, getFileHash } from "./file"; import * as fsNative from "./fsNative"; export interface CompilationConfig extends SandboxConfigWithoutMountInfo { messageFile?: string; // The file contains the message to display for user (in the binary directory) extraInfoFile?: string; // The file contains the extra information for running the compiled program (in the binary directory) workingDirectory: string; // The working directory for the compiler or script } export interface CompileTask { language: string; code: string; compileAndRunOptions: unknown; extraSourceFiles?: Record<string, string>; } async function hashCompileTask(compileTask: CompileTask): Promise<string> { return objectHash({ language: compileTask.language, code: compileTask.code, compileAndRunOptions: compileTask.compileAndRunOptions, extraSourceFiles: compileTask.extraSourceFiles && (await Promise.all( Object.entries(compileTask.extraSourceFiles).map(async ([filename, fileUuid]) => [ filename, await getFileHash(fileUuid) ]) )) }); } export interface CompileResult { compileTaskHash: string; success: boolean; message: OmittableString; } // These class implements reference count to prevent a compile result being deleted // from the disk during using export class CompileResultSuccess implements CompileResult { public readonly success: true = true; constructor( public readonly compileTaskHash: string, public readonly message: OmittableString, public readonly binaryDirectory: string, public readonly binaryDirectorySize: number, public readonly extraInfo: string ) {} // The referenceCount is initially zero, the result must be referenced at least once // Then when dereferenced to zero it will be deleted from the disk private referenceCount: number = 0; public reference() { this.referenceCount++; return this; } public async dereference() { if (--this.referenceCount === 0) { await fsNative.remove(this.binaryDirectory); } }
await fsNative.copy(this.binaryDirectory, newBinaryDirectory); await this.dereference(); return new CompileResultSuccess( this.compileTaskHash, this.message, newBinaryDirectory, this.binaryDirectorySize, this.extraInfo ); } } // Why NOT using the task hash as the directory name? Because there'll be a race condition // If a compile result is disposed from the cache, but still have at least one reference // e.g. referenced by a judge task which have not finished copying the binary files to its working directory // Another cache set operation with the same task will overwrite the files (and may cause the judge task using a corrupted file) // Use a random uuid as the key instead to prevent this class CompileResultCache { private readonly lruCache = new LruCache<string, CompileResultSuccess>({ max: config.binaryCacheMaxSize, length: result => result.binaryDirectorySize, dispose: (compileTaskHash, result) => { winston.verbose(`dispose() from compile result cache: ${compileTaskHash}`); setImmediate(() => { // It's safe NOT to await it.. result.dereference().catch(e => winston.error(`Failed to remove compile result on evicting cache: ${e.stack}`)); }); } }); // The set()/get()'s returned result is reference()-ed // and must be dereference()-ed public get(compileTaskHash: string): CompileResultSuccess { if (this.lruCache.has(compileTaskHash)) return this.lruCache.get(compileTaskHash).reference(); return null; } // set() should not be called twice with the same compileTaskHash in the same time // i.e. call another time with the same compileTaskHash before the previous finished public async set(compileTaskHash: string, result: CompileResultSuccess): Promise<CompileResultSuccess> { if (this.lruCache.has(compileTaskHash)) return this.lruCache.get(compileTaskHash).reference(); const newCompileResult = await result.copyTo(safelyJoinPath(config.binaryCacheStore, uuid())); newCompileResult.reference(); this.lruCache.set(compileTaskHash, newCompileResult); return newCompileResult.reference(); } } interface PendingCompileTask { resultConsumers: ((compileResult: CompileResult) => void)[]; promise: Promise<void>; } // If there're multiple calls to compile() with the same compileTask, it's to prevent the task to be compiled multiple times // compileTaskHash -> Promise of task const pendingCompileTasks: Map<string, PendingCompileTask> = new Map(); const compileResultCache = new CompileResultCache(); export async function compile(compileTask: CompileTask): Promise<CompileResult> { const languageConfig = getLanguage(compileTask.language); const compileTaskHash = await hashCompileTask(compileTask); const cachedResult = compileResultCache.get(compileTaskHash); if (cachedResult) { winston.verbose(`Use cached compile reslt for ${compileTaskHash}`); return cachedResult; } let pendingCompileTask = pendingCompileTasks.get(compileTaskHash); if (!pendingCompileTask) { // Use a array of functions to ensure every calls to compile() of this task could get // a valid CompileResultSuccess object (with positive referenceCount) // I don't think "await promise" is guaranteed to return in a synchronous flow after the promise resolved const resultConsumers = []; pendingCompileTasks.set( compileTaskHash, (pendingCompileTask = { resultConsumers, promise: runTaskQueued(async taskWorkingDirectory => { // The compileResult is already reference()-ed const compileResult = await doCompile(compileTask, compileTaskHash, languageConfig, taskWorkingDirectory); winston.verbose(`Compile result: ${JSON.stringify(compileResult)}`); for (const resultConsumer of resultConsumers) resultConsumer(compileResult instanceof CompileResultSuccess ? compileResult.reference() : compileResult); if (compileResult instanceof CompileResultSuccess) await compileResult.dereference(); }).finally(() => pendingCompileTasks.delete(compileTaskHash)) }) ); } let result: CompileResult; pendingCompileTask.resultConsumers.push(r => { result = r; }); await pendingCompileTask.promise; return result; } // Return reference()-ed result if success async function doCompile( compileTask: CompileTask, compileTaskHash: string, languageConfig: LanguageConfig<unknown>, taskWorkingDirectory: string ): Promise<CompileResult> { const { sourceFilename, binarySizeLimit } = languageConfig.getMetaOptions(compileTask.compileAndRunOptions); const sourceDirectory: MappedPath = { outside: safelyJoinPath(taskWorkingDirectory, "source"), inside: SANDBOX_INSIDE_PATH_SOURCE }; const binaryDirectory: MappedPath = { outside: safelyJoinPath(taskWorkingDirectory, "working"), inside: SANDBOX_INSIDE_PATH_BINARY }; const tempDirectoryOutside = safelyJoinPath(taskWorkingDirectory, "temp"); await Promise.all([ ensureDirectoryEmpty(sourceDirectory.outside), ensureDirectoryEmpty(binaryDirectory.outside), ensureDirectoryEmpty(tempDirectoryOutside) ]); await Promise.all( Object.entries(compileTask.extraSourceFiles || {}).map(([dst, src]) => fs.promises.copyFile(getFile(src), safelyJoinPath(sourceDirectory.outside, dst)) ) ); const sourceFile = safelyJoinPath(sourceDirectory, sourceFilename); await fs.promises.writeFile(sourceFile.outside, compileTask.code); const compileConfig = languageConfig.compile({ sourceDirectoryInside: sourceDirectory.inside, sourcePathInside: sourceFile.inside, binaryDirectoryInside: binaryDirectory.inside, compileAndRunOptions: compileTask.compileAndRunOptions }); // The `taskId` parameter of `runSandbox` is just used to cancel the sandbox // But compilation couldn't be cancelled since multiple submissions may share the same compilation const sandboxResult = await runSandbox(null, { ...compileConfig, tempDirectoryOutside, extraMounts: [ { mappedPath: sourceDirectory, readOnly: true }, { mappedPath: binaryDirectory, readOnly: false } ] }); const messageFile = safelyJoinPath(binaryDirectory, compileConfig.messageFile); const extraInfoFile = compileConfig.extraInfoFile && safelyJoinPath(binaryDirectory, compileConfig.extraInfoFile); const [message, extraInfo] = await Promise.all([ readFileOmitted(messageFile.outside, serverSideConfig.limit.compilerMessage).then(result => result || ""), extraInfoFile ? fsNative .exists(extraInfoFile.outside) .then(exists => (exists ? fs.promises.readFile(extraInfoFile.outside, "utf-8") : null)) : null ]); await Promise.all([ fsNative.remove(messageFile.outside), extraInfoFile ? fsNative.remove(extraInfoFile.outside) : null ]); if (sandboxResult.status === SandboxStatus.OK) { if (sandboxResult.code === 0) {
async copyTo(newBinaryDirectory: string) { this.reference();
random_line_split
supervised_ml_(classification)_assignment_(final).py
how our data is distributes per each column. Though, this doesnt represent much use since some of the attributes are numerical encodings and aren’t represented well like this. In order to see correlations, lets visually interpret this data through a heatmap of correlated values, and a pair plot to draw visual inferences between columns. """ #Description of out dataset, rounded to two decimal places data.describe().round(2) data_corr = data.corr() data_corr plt.figure(figsize=(8,6)) sns.set_context('paper') sns.heatmap(data_corr, annot=True, cmap='Blues', fmt='.0%') """From the above correlation plot we can see correlations between "output" and the following: * cp (43%) * thalachh (42%) * slp (34%) The above attributes represent the highest correlated attributes, thought there are some notable correlations listed below: * age and caa, trtbps * cp and thalachh * chol and age * thalachh and slp, cp * exng and oldpeak * oldpeak and caa * thall and exnp, oldpeak, age """ #broad look at data distribution sns.pairplot(data) """The below plots depict the density of data in accordance with what would be a high likely hood and a low likelihood of heart attack risk. This gives insight to the shape of the data given the output of events.""" X = data.drop('output',axis=1) y = data['output'] riskyDF = data[y == 1] safeDF = data[y == 0] for col in data.select_dtypes(include=['float64','int64']): plt.figure(figsize=(4,4)) sns.distplot(riskyDF[col],label='High Risk') sns.distplot(safeDF[col],label='Low Risk') plt.legend() plt.show() """--- # Data Engineering/Modelling Because the data is already in a numerical form (int-type), it will not be required to engineer the data or reencode values. Though, given the tasks ahead, we may require data scaling for input into specific classifier models. We shall address this problem as we arrive to it. But for now, we can get to creating our train-test split. """ #train test splitting y = data['output'] x = data.drop('output', axis=1) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state=42) """--- # Classifier Model 1: KNN First let’s explore using K-Nearest Neighbour (KNN) algorithm. The KNN algorithm assumes that similar things exist in proximity. In other words, similar things are near to each other. KNN algorithm assumes the similarity between the new case/data and available cases and put the new case into the category that is most like the available categories. For this algorithm to work we need to scale the test/train data. To do this, I shall generate a pipeline. This will allow scaling the train/test data without taking up additional resources or modifying the original test/train split, as well as keep the use of our train/test split uniform across the other two classification models. """ KNN_pipeline = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier())]) KNN_pipeline.fit(x_train, y_train) KNN_pipeline.score(x_test, y_test) y_proba = KNN_pipeline.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """Can we increase our accuracy of our KNN Classifier?! Let's find out below by running the pipeline through a for-loop, increasing the number of neighbours for selection, and plotting the accuracy out to see the change in value.""" err = [] for i in range(1, 40): model = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = i))]) model.fit(x_train, y_train) pred_i = model.predict(x_test) err.append(np.mean(pred_i != y_test)) plt.figure(figsize =(10, 8)) plt.plot(range(1, 40), err, color ='blue', linestyle ='dashed', marker ='o', markerfacecolor ='blue', markersize = 8) plt.title('Mean Err = f(K)') plt.xlabel('K') plt.ylabel('Mean Err') """The above output suggests that a value of "5" should be the most optimized value.""" KNN_pipeline_Opt = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = 5))]) KNN_pipeline_Opt.fit(x_train, y_train) y_proba = KNN_pipeline_Opt.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """However, this seems to be negligible since the resultant accuracy has no increased any more than the first model. --- # Classifier Model 2: Decision Tree Classification A decision tree is a decision support tool that uses a tree-like model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. It is one way to display an algorithm that only contains conditional control statements. However for this use case, we are going to use it to classify attributes to create a process of decision to predict a patients possibility of a heart attack. """ dt = DecisionTreeClassifier(random_state=42) dt = dt.fit(x_train, y_train) dt.tree_.node_count, dt.tree_.max_depth # The error on the training and test data sets (Taken from workbook) y_train_pred = dt.predict(x_train) y_test_pred = dt.predict(x_test) def measure_error(y_true, y_pred, label): return pd.Series({'accuracy':accuracy_score(y_true, y_pred), 'precision': precision_score(y_true, y_pred), 'recall': recall_score(y_true, y_pred), 'f1': f1_score(y_true, y_pred)}, name=label) train_test_full_error = pd.concat([measure_error(y_train, y_train_pred, 'train'), measure_error(y_test, y_test_pred, 'test')], axis=1) train_test_full_error """The above output shows out accuracy prediction. This is quite low, could it be improved with Grid Search Cross Validation (GSCV)?! Let's find out below:""" # Grid Search optimization param_grid = {'max_depth':range(1, dt.tree_.max_depth+1, 2), 'max_features': range(1, len(dt.feature_importances_)+1)} GR = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid=param_grid, scoring='accuracy', n_jobs=-1) GR = GR.fit(x_train, y_train) GR.best_estimator_.tree_.node_count, GR.best_estimator_.tree_.max_depth y_train_pred_gr = GR.predict(x_train) y_test_pred_gr = GR.predict(x_test) train_test_gr_error = pd.concat([measure_error(y_train, y_train_pred_gr, 'train'), measure_error(y_test, y_test_pred_gr, 'test')], axis=1) train_test_gr_error """We have increased the accuracy by 0.01 but at the cost of our precision. Also, our recall has increased significantly. Let’s look at the two different trees:""" # Create an output destination for the file dot_data = StringIO() export_graphviz(dt, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_1.png' graph.write_png(filename) Image(filename=filename) # Create an output destination for the file dot_data = StringIO() export_graphviz(GR.best_estimator_, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_2.png' graph.write_png(filename) Image(filename=filename) ### END SOLUTION """--- # Classifier Model 3: Random Forest Classification Random forest is an ensemble machine learning algorithm. A forest is comprised of trees. It is said that the more trees it has, the more robust a forest is. Random forests create decision trees on randomly selected data samples, gets prediction from each tree and selects the best solution by means of voting. It also provides a pretty good indicator of the feature importance. Let’s see how well our heart attack data is classified by Random Forest. """ # Initialize the random forest estimator # Note that the number of trees is not setup here RF = RandomForestClassifier(oob_score=True, random_state=42, warm_start=True, n_jobs=-1) oob_list = list() # Iterate through all of the possibilities for # number of trees for n_trees in [15, 20, 30, 40, 50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 900, 1000]: # Use this to set the number of trees RF.set_par
ams(n_estimators=n_trees) # Fit the model RF.fit(x_train, y_train) # Get the oob error oob_error = 1 - RF.oob_score_ # Store it oob_list.append(pd.Series({'n_trees': n_trees, 'oob': oob_error})) rf_oob_d
conditional_block
supervised_ml_(classification)_assignment_(final).py
* Value 2: showing probable or definite left ventricular hypertrophy by Estes' criteria * thalach : maximum heart rate achieved * target : 0= less chance of heart attack 1= more chance of heart attack Let's begin by importing our data into pandas. """ #path must point to the heart.csv file. data = pd.read_csv('/content/heart.csv', index_col=None, header=0) data.head() data.shape """--- # Data Cleaning Before we start exploring our dataset, lets first do some simple checks to see if we need to do anything. Below, we shall check our datatypes, see if there are any missing values in each row, and check for any duplicate values. """ #Count missing values in each column. data.isna().sum() #duplicate values check data.duplicated().sum() data.loc[data.duplicated(keep=False),:] """As we can see, we are primarily working with "Int"-type values. This means we do not require any more work to change the features of the data. We also have no absent attributes in each row, but we have discovered duplicate values. All we need to do is just drop this value and continue forward.""" #drop duplicates data.drop_duplicates(keep='first', inplace=True) """--- # Data Exploration Let’s look at our data and see any noticeable correlations. The below table describes how our data is distributes per each column. Though, this doesnt represent much use since some of the attributes are numerical encodings and aren’t represented well like this. In order to see correlations, lets visually interpret this data through a heatmap of correlated values, and a pair plot to draw visual inferences between columns. """ #Description of out dataset, rounded to two decimal places data.describe().round(2) data_corr = data.corr() data_corr plt.figure(figsize=(8,6)) sns.set_context('paper') sns.heatmap(data_corr, annot=True, cmap='Blues', fmt='.0%') """From the above correlation plot we can see correlations between "output" and the following: * cp (43%) * thalachh (42%) * slp (34%) The above attributes represent the highest correlated attributes, thought there are some notable correlations listed below: * age and caa, trtbps * cp and thalachh * chol and age * thalachh and slp, cp * exng and oldpeak * oldpeak and caa * thall and exnp, oldpeak, age """ #broad look at data distribution sns.pairplot(data) """The below plots depict the density of data in accordance with what would be a high likely hood and a low likelihood of heart attack risk. This gives insight to the shape of the data given the output of events.""" X = data.drop('output',axis=1) y = data['output'] riskyDF = data[y == 1] safeDF = data[y == 0] for col in data.select_dtypes(include=['float64','int64']): plt.figure(figsize=(4,4)) sns.distplot(riskyDF[col],label='High Risk') sns.distplot(safeDF[col],label='Low Risk') plt.legend()
plt.show() """--- # Data Engineering/Modelling Because the data is already in a numerical form (int-type), it will not be required to engineer the data or reencode values. Though, given the tasks ahead, we may require data scaling for input into specific classifier models. We shall address this problem as we arrive to it. But for now, we can get to creating our train-test split. """ #train test splitting y = data['output'] x = data.drop('output', axis=1) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state=42) """--- # Classifier Model 1: KNN First let’s explore using K-Nearest Neighbour (KNN) algorithm. The KNN algorithm assumes that similar things exist in proximity. In other words, similar things are near to each other. KNN algorithm assumes the similarity between the new case/data and available cases and put the new case into the category that is most like the available categories. For this algorithm to work we need to scale the test/train data. To do this, I shall generate a pipeline. This will allow scaling the train/test data without taking up additional resources or modifying the original test/train split, as well as keep the use of our train/test split uniform across the other two classification models. """ KNN_pipeline = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier())]) KNN_pipeline.fit(x_train, y_train) KNN_pipeline.score(x_test, y_test) y_proba = KNN_pipeline.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """Can we increase our accuracy of our KNN Classifier?! Let's find out below by running the pipeline through a for-loop, increasing the number of neighbours for selection, and plotting the accuracy out to see the change in value.""" err = [] for i in range(1, 40): model = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = i))]) model.fit(x_train, y_train) pred_i = model.predict(x_test) err.append(np.mean(pred_i != y_test)) plt.figure(figsize =(10, 8)) plt.plot(range(1, 40), err, color ='blue', linestyle ='dashed', marker ='o', markerfacecolor ='blue', markersize = 8) plt.title('Mean Err = f(K)') plt.xlabel('K') plt.ylabel('Mean Err') """The above output suggests that a value of "5" should be the most optimized value.""" KNN_pipeline_Opt = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = 5))]) KNN_pipeline_Opt.fit(x_train, y_train) y_proba = KNN_pipeline_Opt.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """However, this seems to be negligible since the resultant accuracy has no increased any more than the first model. --- # Classifier Model 2: Decision Tree Classification A decision tree is a decision support tool that uses a tree-like model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. It is one way to display an algorithm that only contains conditional control statements. However for this use case, we are going to use it to classify attributes to create a process of decision to predict a patients possibility of a heart attack. """ dt = DecisionTreeClassifier(random_state=42) dt = dt.fit(x_train, y_train) dt.tree_.node_count, dt.tree_.max_depth # The error on the training and test data sets (Taken from workbook) y_train_pred = dt.predict(x_train) y_test_pred = dt.predict(x_test) def measure_error(y_true, y_pred, label): return pd.Series({'accuracy':accuracy_score(y_true, y_pred), 'precision': precision_score(y_true, y_pred), 'recall': recall_score(y_true, y_pred), 'f1': f1_score(y_true, y_pred)}, name=label) train_test_full_error = pd.concat([measure_error(y_train, y_train_pred, 'train'), measure_error(y_test, y_test_pred, 'test')], axis=1) train_test_full_error """The above output shows out accuracy prediction. This is quite low, could it be improved with Grid Search Cross Validation (GSCV)?! Let's find out below:""" # Grid Search optimization param_grid = {'max_depth':range(1, dt.tree_.max_depth+1, 2), 'max_features': range(1, len(dt.feature_importances_)+1)} GR = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid=param_grid, scoring='accuracy', n_jobs=-1) GR = GR.fit(x_train, y_train) GR.best_estimator_.tree_.node_count, GR.best_estimator_.tree_.max_depth y_train_pred_gr = GR.predict(x_train) y_test_pred_gr = GR.predict(x_test) train_test_gr_error = pd.concat([measure_error(y_train, y_train_pred_gr, 'train'), measure_error(y_test, y_test_pred_gr, 'test')], axis=1) train_test_gr_error """We have increased the accuracy by 0.01 but at the cost of our precision. Also, our recall has increased significantly. Let’s look at the two different trees:""" # Create an output destination for the file dot_data = StringIO() export_graphviz(dt, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_1.png' graph.write_png(filename) Image(filename=filename) # Create an output destination for the file dot_data = StringIO() export_graphviz(GR.best_estimator_, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_2.png' graph.write_png(filename) Image(filename=filename) ### END SOLUTION """--- # Classifier Model 3: Random Forest Classification Random forest is an ensemble machine learning algorithm. A forest is comprised of trees. It is said that the more trees it has, the
random_line_split
supervised_ml_(classification)_assignment_(final).py
Value 2: showing probable or definite left ventricular hypertrophy by Estes' criteria * thalach : maximum heart rate achieved * target : 0= less chance of heart attack 1= more chance of heart attack Let's begin by importing our data into pandas. """ #path must point to the heart.csv file. data = pd.read_csv('/content/heart.csv', index_col=None, header=0) data.head() data.shape """--- # Data Cleaning Before we start exploring our dataset, lets first do some simple checks to see if we need to do anything. Below, we shall check our datatypes, see if there are any missing values in each row, and check for any duplicate values. """ #Count missing values in each column. data.isna().sum() #duplicate values check data.duplicated().sum() data.loc[data.duplicated(keep=False),:] """As we can see, we are primarily working with "Int"-type values. This means we do not require any more work to change the features of the data. We also have no absent attributes in each row, but we have discovered duplicate values. All we need to do is just drop this value and continue forward.""" #drop duplicates data.drop_duplicates(keep='first', inplace=True) """--- # Data Exploration Let’s look at our data and see any noticeable correlations. The below table describes how our data is distributes per each column. Though, this doesnt represent much use since some of the attributes are numerical encodings and aren’t represented well like this. In order to see correlations, lets visually interpret this data through a heatmap of correlated values, and a pair plot to draw visual inferences between columns. """ #Description of out dataset, rounded to two decimal places data.describe().round(2) data_corr = data.corr() data_corr plt.figure(figsize=(8,6)) sns.set_context('paper') sns.heatmap(data_corr, annot=True, cmap='Blues', fmt='.0%') """From the above correlation plot we can see correlations between "output" and the following: * cp (43%) * thalachh (42%) * slp (34%) The above attributes represent the highest correlated attributes, thought there are some notable correlations listed below: * age and caa, trtbps * cp and thalachh * chol and age * thalachh and slp, cp * exng and oldpeak * oldpeak and caa * thall and exnp, oldpeak, age """ #broad look at data distribution sns.pairplot(data) """The below plots depict the density of data in accordance with what would be a high likely hood and a low likelihood of heart attack risk. This gives insight to the shape of the data given the output of events.""" X = data.drop('output',axis=1) y = data['output'] riskyDF = data[y == 1] safeDF = data[y == 0] for col in data.select_dtypes(include=['float64','int64']): plt.figure(figsize=(4,4)) sns.distplot(riskyDF[col],label='High Risk') sns.distplot(safeDF[col],label='Low Risk') plt.legend() plt.show() """--- # Data Engineering/Modelling Because the data is already in a numerical form (int-type), it will not be required to engineer the data or reencode values. Though, given the tasks ahead, we may require data scaling for input into specific classifier models. We shall address this problem as we arrive to it. But for now, we can get to creating our train-test split. """ #train test splitting y = data['output'] x = data.drop('output', axis=1) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state=42) """--- # Classifier Model 1: KNN First let’s explore using K-Nearest Neighbour (KNN) algorithm. The KNN algorithm assumes that similar things exist in proximity. In other words, similar things are near to each other. KNN algorithm assumes the similarity between the new case/data and available cases and put the new case into the category that is most like the available categories. For this algorithm to work we need to scale the test/train data. To do this, I shall generate a pipeline. This will allow scaling the train/test data without taking up additional resources or modifying the original test/train split, as well as keep the use of our train/test split uniform across the other two classification models. """ KNN_pipeline = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier())]) KNN_pipeline.fit(x_train, y_train) KNN_pipeline.score(x_test, y_test) y_proba = KNN_pipeline.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """Can we increase our accuracy of our KNN Classifier?! Let's find out below by running the pipeline through a for-loop, increasing the number of neighbours for selection, and plotting the accuracy out to see the change in value.""" err = [] for i in range(1, 40): model = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = i))]) model.fit(x_train, y_train) pred_i = model.predict(x_test) err.append(np.mean(pred_i != y_test)) plt.figure(figsize =(10, 8)) plt.plot(range(1, 40), err, color ='blue', linestyle ='dashed', marker ='o', markerfacecolor ='blue', markersize = 8) plt.title('Mean Err = f(K)') plt.xlabel('K') plt.ylabel('Mean Err') """The above output suggests that a value of "5" should be the most optimized value.""" KNN_pipeline_Opt = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = 5))]) KNN_pipeline_Opt.fit(x_train, y_train) y_proba = KNN_pipeline_Opt.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """However, this seems to be negligible since the resultant accuracy has no increased any more than the first model. --- # Classifier Model 2: Decision Tree Classification A decision tree is a decision support tool that uses a tree-like model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. It is one way to display an algorithm that only contains conditional control statements. However for this use case, we are going to use it to classify attributes to create a process of decision to predict a patients possibility of a heart attack. """ dt = DecisionTreeClassifier(random_state=42) dt = dt.fit(x_train, y_train) dt.tree_.node_count, dt.tree_.max_depth # The error on the training and test data sets (Taken from workbook) y_train_pred = dt.predict(x_train) y_test_pred = dt.predict(x_test) def measur
e, y_pred, label): return pd.Series({'accuracy':accuracy_score(y_true, y_pred), 'precision': precision_score(y_true, y_pred), 'recall': recall_score(y_true, y_pred), 'f1': f1_score(y_true, y_pred)}, name=label) train_test_full_error = pd.concat([measure_error(y_train, y_train_pred, 'train'), measure_error(y_test, y_test_pred, 'test')], axis=1) train_test_full_error """The above output shows out accuracy prediction. This is quite low, could it be improved with Grid Search Cross Validation (GSCV)?! Let's find out below:""" # Grid Search optimization param_grid = {'max_depth':range(1, dt.tree_.max_depth+1, 2), 'max_features': range(1, len(dt.feature_importances_)+1)} GR = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid=param_grid, scoring='accuracy', n_jobs=-1) GR = GR.fit(x_train, y_train) GR.best_estimator_.tree_.node_count, GR.best_estimator_.tree_.max_depth y_train_pred_gr = GR.predict(x_train) y_test_pred_gr = GR.predict(x_test) train_test_gr_error = pd.concat([measure_error(y_train, y_train_pred_gr, 'train'), measure_error(y_test, y_test_pred_gr, 'test')], axis=1) train_test_gr_error """We have increased the accuracy by 0.01 but at the cost of our precision. Also, our recall has increased significantly. Let’s look at the two different trees:""" # Create an output destination for the file dot_data = StringIO() export_graphviz(dt, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_1.png' graph.write_png(filename) Image(filename=filename) # Create an output destination for the file dot_data = StringIO() export_graphviz(GR.best_estimator_, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_2.png' graph.write_png(filename) Image(filename=filename) ### END SOLUTION """--- # Classifier Model 3: Random Forest Classification Random forest is an ensemble machine learning algorithm. A forest is comprised of trees. It is said that the more trees it has
e_error(y_tru
identifier_name
supervised_ml_(classification)_assignment_(final).py
Value 2: showing probable or definite left ventricular hypertrophy by Estes' criteria * thalach : maximum heart rate achieved * target : 0= less chance of heart attack 1= more chance of heart attack Let's begin by importing our data into pandas. """ #path must point to the heart.csv file. data = pd.read_csv('/content/heart.csv', index_col=None, header=0) data.head() data.shape """--- # Data Cleaning Before we start exploring our dataset, lets first do some simple checks to see if we need to do anything. Below, we shall check our datatypes, see if there are any missing values in each row, and check for any duplicate values. """ #Count missing values in each column. data.isna().sum() #duplicate values check data.duplicated().sum() data.loc[data.duplicated(keep=False),:] """As we can see, we are primarily working with "Int"-type values. This means we do not require any more work to change the features of the data. We also have no absent attributes in each row, but we have discovered duplicate values. All we need to do is just drop this value and continue forward.""" #drop duplicates data.drop_duplicates(keep='first', inplace=True) """--- # Data Exploration Let’s look at our data and see any noticeable correlations. The below table describes how our data is distributes per each column. Though, this doesnt represent much use since some of the attributes are numerical encodings and aren’t represented well like this. In order to see correlations, lets visually interpret this data through a heatmap of correlated values, and a pair plot to draw visual inferences between columns. """ #Description of out dataset, rounded to two decimal places data.describe().round(2) data_corr = data.corr() data_corr plt.figure(figsize=(8,6)) sns.set_context('paper') sns.heatmap(data_corr, annot=True, cmap='Blues', fmt='.0%') """From the above correlation plot we can see correlations between "output" and the following: * cp (43%) * thalachh (42%) * slp (34%) The above attributes represent the highest correlated attributes, thought there are some notable correlations listed below: * age and caa, trtbps * cp and thalachh * chol and age * thalachh and slp, cp * exng and oldpeak * oldpeak and caa * thall and exnp, oldpeak, age """ #broad look at data distribution sns.pairplot(data) """The below plots depict the density of data in accordance with what would be a high likely hood and a low likelihood of heart attack risk. This gives insight to the shape of the data given the output of events.""" X = data.drop('output',axis=1) y = data['output'] riskyDF = data[y == 1] safeDF = data[y == 0] for col in data.select_dtypes(include=['float64','int64']): plt.figure(figsize=(4,4)) sns.distplot(riskyDF[col],label='High Risk') sns.distplot(safeDF[col],label='Low Risk') plt.legend() plt.show() """--- # Data Engineering/Modelling Because the data is already in a numerical form (int-type), it will not be required to engineer the data or reencode values. Though, given the tasks ahead, we may require data scaling for input into specific classifier models. We shall address this problem as we arrive to it. But for now, we can get to creating our train-test split. """ #train test splitting y = data['output'] x = data.drop('output', axis=1) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state=42) """--- # Classifier Model 1: KNN First let’s explore using K-Nearest Neighbour (KNN) algorithm. The KNN algorithm assumes that similar things exist in proximity. In other words, similar things are near to each other. KNN algorithm assumes the similarity between the new case/data and available cases and put the new case into the category that is most like the available categories. For this algorithm to work we need to scale the test/train data. To do this, I shall generate a pipeline. This will allow scaling the train/test data without taking up additional resources or modifying the original test/train split, as well as keep the use of our train/test split uniform across the other two classification models. """ KNN_pipeline = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier())]) KNN_pipeline.fit(x_train, y_train) KNN_pipeline.score(x_test, y_test) y_proba = KNN_pipeline.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """Can we increase our accuracy of our KNN Classifier?! Let's find out below by running the pipeline through a for-loop, increasing the number of neighbours for selection, and plotting the accuracy out to see the change in value.""" err = [] for i in range(1, 40): model = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = i))]) model.fit(x_train, y_train) pred_i = model.predict(x_test) err.append(np.mean(pred_i != y_test)) plt.figure(figsize =(10, 8)) plt.plot(range(1, 40), err, color ='blue', linestyle ='dashed', marker ='o', markerfacecolor ='blue', markersize = 8) plt.title('Mean Err = f(K)') plt.xlabel('K') plt.ylabel('Mean Err') """The above output suggests that a value of "5" should be the most optimized value.""" KNN_pipeline_Opt = Pipeline([('ss', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors = 5))]) KNN_pipeline_Opt.fit(x_train, y_train) y_proba = KNN_pipeline_Opt.predict_proba(x_test) y_pred = np.argmax(y_proba,axis=1) print("KNN : ", accuracy_score(y_test, y_pred)) """However, this seems to be negligible since the resultant accuracy has no increased any more than the first model. --- # Classifier Model 2: Decision Tree Classification A decision tree is a decision support tool that uses a tree-like model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. It is one way to display an algorithm that only contains conditional control statements. However for this use case, we are going to use it to classify attributes to create a process of decision to predict a patients possibility of a heart attack. """ dt = DecisionTreeClassifier(random_state=42) dt = dt.fit(x_train, y_train) dt.tree_.node_count, dt.tree_.max_depth # The error on the training and test data sets (Taken from workbook) y_train_pred = dt.predict(x_train) y_test_pred = dt.predict(x_test) def measure_error(y_true, y_pred, label): return
n_test_full_error = pd.concat([measure_error(y_train, y_train_pred, 'train'), measure_error(y_test, y_test_pred, 'test')], axis=1) train_test_full_error """The above output shows out accuracy prediction. This is quite low, could it be improved with Grid Search Cross Validation (GSCV)?! Let's find out below:""" # Grid Search optimization param_grid = {'max_depth':range(1, dt.tree_.max_depth+1, 2), 'max_features': range(1, len(dt.feature_importances_)+1)} GR = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid=param_grid, scoring='accuracy', n_jobs=-1) GR = GR.fit(x_train, y_train) GR.best_estimator_.tree_.node_count, GR.best_estimator_.tree_.max_depth y_train_pred_gr = GR.predict(x_train) y_test_pred_gr = GR.predict(x_test) train_test_gr_error = pd.concat([measure_error(y_train, y_train_pred_gr, 'train'), measure_error(y_test, y_test_pred_gr, 'test')], axis=1) train_test_gr_error """We have increased the accuracy by 0.01 but at the cost of our precision. Also, our recall has increased significantly. Let’s look at the two different trees:""" # Create an output destination for the file dot_data = StringIO() export_graphviz(dt, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_1.png' graph.write_png(filename) Image(filename=filename) # Create an output destination for the file dot_data = StringIO() export_graphviz(GR.best_estimator_, out_file=dot_data, filled=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) # View the tree image filename = 'tree_2.png' graph.write_png(filename) Image(filename=filename) ### END SOLUTION """--- # Classifier Model 3: Random Forest Classification Random forest is an ensemble machine learning algorithm. A forest is comprised of trees. It is said that the more trees it has,
pd.Series({'accuracy':accuracy_score(y_true, y_pred), 'precision': precision_score(y_true, y_pred), 'recall': recall_score(y_true, y_pred), 'f1': f1_score(y_true, y_pred)}, name=label) trai
identifier_body
tls.go
scores = conf.categoryScores(tally) reqACLs = conf.ACLs.requestACLs(cr, authUser) if invalidSSL { reqACLs["invalid-ssl"] = true } if r == nil { // It's a transparently-intercepted request instead of a real // CONNECT request. reqACLs["transparent"] = true } } session.ACLs.data = reqACLs session.Scores.data = scores session.PossibleActions = []string{"allow", "block"} if getConfig().TLSReady && !obsoleteVersion && !invalidSSL
callStarlarkFunctions("ssl_bump", session) dialer := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, DualStack: true, } if session.SourceIP != nil { dialer.LocalAddr = &net.TCPAddr{ IP: session.SourceIP, } } session.chooseAction() logAccess(cr, nil, 0, false, user, tally, scores, session.Action, "", session.Ignored, nil) switch session.Action.Action { case "allow", "": upload, download := connectDirect(conn, session.ServerAddr, clientHello, dialer) logAccess(cr, nil, upload+download, false, user, tally, scores, session.Action, "", session.Ignored, nil) return case "block": conn.Close() return } var cert tls.Certificate var rt http.RoundTripper var http2Support bool closeChan := make(chan struct{}) server := &http.Server{ IdleTimeout: getConfig().CloseIdleConnections, ConnState: func(conn net.Conn, state http.ConnState) { switch state { case http.StateClosed: close(closeChan) } }, } serverConnConfig := &tls.Config{ ServerName: session.SNI, InsecureSkipVerify: true, } clientSupportsHTTP2 := false if clientHelloInfo != nil { for _, p := range clientHelloInfo.SupportedProtos { if p == "h2" { clientSupportsHTTP2 = true } } } if clientSupportsHTTP2 && getConfig().HTTP2Upstream { serverConnConfig.NextProtos = []string{"h2", "http/1.1"} } serverConn, err := tls.DialWithDialer(dialer, "tcp", session.ServerAddr, serverConnConfig) if err == nil { defer serverConn.Close() state := serverConn.ConnectionState() serverCert := state.PeerCertificates[0] valid := validCert(serverCert, state.PeerCertificates[1:]) cert, err = imitateCertificate(serverCert, !valid, session.SNI) if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error generating certificate: %v", err), false, tlsFingerprint) connectDirect(conn, session.ServerAddr, clientHello, dialer) return } http2Support = state.NegotiatedProtocol == "h2" && state.NegotiatedProtocolIsMutual d := &tls.Dialer{ NetDialer: dialer, Config: &tls.Config{ ServerName: session.SNI, RootCAs: certPoolWith(serverConn.ConnectionState().PeerCertificates), }, } if !valid { d.Config.InsecureSkipVerify = true originalCert := serverConn.ConnectionState().PeerCertificates[0] d.Config.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error { cert, err := x509.ParseCertificate(rawCerts[0]) if err != nil { return err } if cert.Equal(originalCert) { return nil } return errCertMismatch } } if http2Support { d.Config.NextProtos = []string{"h2"} var once sync.Once rt = &http2.Transport{ DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { var c net.Conn once.Do(func() { c = serverConn }) if c != nil { return c, nil } logVerbose("redial", "Redialing HTTP/2 connection to %s (%s)", session.SNI, session.ServerAddr) return d.Dial("tcp", session.ServerAddr) }, TLSClientConfig: d.Config, StrictMaxConcurrentStreams: true, } } else { rt = &connTransport{ Conn: serverConn, Redial: func(ctx context.Context) (net.Conn, error) { logVerbose("redial", "Redialing connection to %s (%s)", session.SNI, session.ServerAddr) return d.DialContext(ctx, "tcp", session.ServerAddr) }, } } } else { cert, err = fakeCertificate(session.SNI) if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error generating certificate: %v", err), false, tlsFingerprint) conn.Close() return } rt = httpTransport } session.Freeze() server.Handler = &proxyHandler{ TLS: true, tlsFingerprint: tlsFingerprint, connectPort: port, user: authUser, rt: rt, session: session, } tlsConfig := &tls.Config{ Certificates: []tls.Certificate{cert, getConfig().TLSCert}, PreferServerCipherSuites: true, CurvePreferences: []tls.CurveID{ tls.CurveP256, tls.X25519, // Go 1.8 only }, } http2Downstream := getConfig().HTTP2Downstream && http2Support if http2Downstream { tlsConfig.NextProtos = []string{"h2", "http/1.1"} } tlsConn := tls.Server(&insertingConn{conn, clientHello}, tlsConfig) err = tlsConn.Handshake() if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error in handshake with client: %v", err), false, tlsFingerprint) conn.Close() return } logTLS(user, session.ServerAddr, serverName, nil, false, tlsFingerprint) if http2Downstream { http2.ConfigureServer(server, nil) } listener := &singleListener{conn: tlsConn} server.Serve(listener) // Wait for the connection to finish. <-closeChan } // A TLSSession is the parameter for the Starlark ssl_bump function. type TLSSession struct { SNI string ServerAddr string User string ClientIP string // SourceIP is the IP address of the network interface to be used fo dial // the upstream connection. SourceIP net.IP // ConnectHeader is the header from the CONNECT request, if any. ConnectHeader http.Header scoresAndACLs frozen bool misc SyncDict } type scoresAndACLs struct { ACLs StringSet Scores StringIntDict Tally map[rule]int PossibleActions []string Action ACLActionRule Ignored []string } func (s *scoresAndACLs) currentAction() (ar ACLActionRule, ignored []string) { if s.Action.Action != "" { return s.Action, s.Ignored } conf := getConfig() ar, ignored = conf.ChooseACLCategoryAction(s.ACLs.data, s.Scores.data, conf.Threshold, s.PossibleActions...) if ar.Action == "" { ar.Action = "allow" } return ar, ignored } func (s *scoresAndACLs) chooseAction() { s.Action, s.Ignored = s.currentAction() } func (s *scoresAndACLs) setAction(newAction string) error { for _, a := range s.PossibleActions { if newAction == a { s.Action = ACLActionRule{ Action: newAction, Needed: []string{"starlark"}, } return nil } } return fmt.Errorf("can't set action to %q; expected one of %q", newAction, s.PossibleActions) } func (s *TLSSession) String() string { return fmt.Sprintf("TLSSession(%q, %q)", s.SNI, s.ServerAddr) } func (s *TLSSession) Type() string { return "TLSSession" } func (s *TLSSession) Freeze() { if !s.frozen { s.frozen = true s.ACLs.Freeze() s.Scores.Freeze() } } func (s *TLSSession) Truth() starlark.Bool { return starlark.True } func (s *TLSSession) Hash() (uint32, error) { return 0,
{ session.PossibleActions = append(session.PossibleActions, "ssl-bump") }
conditional_block
tls.go
0, 0, time.Local), KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, DNSNames: []string{sni}, SignatureAlgorithm: x509.UnknownSignatureAlgorithm, } newCertBytes, err := x509.CreateCertificate(rand.Reader, template, conf.ParsedTLSCert, conf.ParsedTLSCert.PublicKey, conf.TLSCert.PrivateKey) if err != nil { return tls.Certificate{}, err } newCert := tls.Certificate{ Certificate: [][]byte{newCertBytes}, PrivateKey: conf.TLSCert.PrivateKey, } newCert.Certificate = append(newCert.Certificate, conf.TLSCert.Certificate...) return newCert, nil } func validCert(cert *x509.Certificate, intermediates []*x509.Certificate) bool { conf := getConfig() pool := certPoolWith(intermediates) _, err := cert.Verify(x509.VerifyOptions{Intermediates: pool}) if err == nil { return true } if _, ok := err.(x509.UnknownAuthorityError); !ok { // There was an error, but not because the certificate wasn't signed // by a recognized CA. So we go ahead and use the cert and let // the client experience the same error. return true } if conf.ExtraRootCerts != nil { _, err = cert.Verify(x509.VerifyOptions{Roots: conf.ExtraRootCerts, Intermediates: pool}) if err == nil { return true } if _, ok := err.(x509.UnknownAuthorityError); !ok { return true } } // Before we give up, we'll try fetching some intermediate certificates. if len(cert.IssuingCertificateURL) == 0 { return false } toFetch := cert.IssuingCertificateURL fetched := make(map[string]bool) for i := 0; i < len(toFetch); i++ { certURL := toFetch[i] if fetched[certURL] { continue } resp, err := http.Get(certURL) if err == nil { defer resp.Body.Close() } if err != nil || resp.StatusCode != 200 { continue } fetchedCert, err := ioutil.ReadAll(resp.Body) if err != nil { continue } // The fetched certificate might be in either DER or PEM format. if bytes.Contains(fetchedCert, []byte("-----BEGIN CERTIFICATE-----")) { // It's PEM. var certDER *pem.Block for { certDER, fetchedCert = pem.Decode(fetchedCert) if certDER == nil { break } if certDER.Type != "CERTIFICATE" { continue } thisCert, err := x509.ParseCertificate(certDER.Bytes) if err != nil { continue } pool.AddCert(thisCert) toFetch = append(toFetch, thisCert.IssuingCertificateURL...) } } else { // Hopefully it's DER. thisCert, err := x509.ParseCertificate(fetchedCert) if err != nil { continue } pool.AddCert(thisCert) toFetch = append(toFetch, thisCert.IssuingCertificateURL...) } } _, err = cert.Verify(x509.VerifyOptions{Intermediates: pool}) if err == nil { return true } if _, ok := err.(x509.UnknownAuthorityError); !ok { // There was an error, but not because the certificate wasn't signed // by a recognized CA. So we go ahead and use the cert and let // the client experience the same error. return true } return false } var ErrObsoleteSSLVersion = errors.New("obsolete SSL protocol version") var ErrInvalidSSL = errors.New("invalid first byte for SSL connection; possibly some other protocol") func readClientHello(conn net.Conn) (hello []byte, err error) { conn.SetReadDeadline(time.Now().Add(10 * time.Second)) defer conn.SetReadDeadline(time.Time{}) var header [5]byte n, err := io.ReadFull(conn, header[:]) hello = header[:n] if err != nil { return hello, err } if header[0] != 22 { if header[0] == 128 { return hello, ErrObsoleteSSLVersion } return hello, ErrInvalidSSL } if header[1] != 3 { return hello, fmt.Errorf("expected major version of 3, got %d", header[1]) } recordLen := int(header[3])<<8 | int(header[4]) if recordLen > 0x3000 { return hello, fmt.Errorf("expected length less than 12kB, got %d", recordLen) } if recordLen < 4 { return hello, fmt.Errorf("expected length of at least 4 bytes, got %d", recordLen) } protocolData := make([]byte, recordLen) n, err = io.ReadFull(conn, protocolData) hello = append(hello, protocolData[:n]...) if err != nil { return hello, err } if protocolData[0] != 1 { return hello, fmt.Errorf("Expected message type 1 (ClientHello), got %d", protocolData[0]) } protocolLen := int(protocolData[1])<<16 | int(protocolData[2])<<8 | int(protocolData[3]) if protocolLen != recordLen-4 { return hello, fmt.Errorf("recordLen=%d, protocolLen=%d", recordLen, protocolLen) } return hello, nil } // parseClientHello parses some useful information out of a ClientHello message. // It returns a ClientHelloInfo with only the following fields filled in: // ServerName and SupportedProtocols. func parseClientHello(data []byte) (*tls.ClientHelloInfo, error) { // The implementation of this function is based on crypto/tls.clientHelloMsg.unmarshal var info tls.ClientHelloInfo s := cryptobyte.String(data) // Skip message type, length, version, and random. if !s.Skip(43) { return nil, errors.New("too short") } var sessionID cryptobyte.String if !s.ReadUint8LengthPrefixed(&sessionID) { return nil, errors.New("bad session ID") } var cipherSuites cryptobyte.String if !s.ReadUint16LengthPrefixed(&cipherSuites) { return nil, errors.New("bad cipher suites") } var compressionMethods cryptobyte.String if !s.ReadUint8LengthPrefixed(&compressionMethods) { return nil, errors.New("bad compression methods") } if s.Empty() { // no extensions return &info, nil } var extensions cryptobyte.String if !s.ReadUint16LengthPrefixed(&extensions) || !s.Empty() { return nil, errors.New("bad extensions") } for !extensions.Empty() { var extension uint16 var extData cryptobyte.String if !extensions.ReadUint16(&extension) || !extensions.ReadUint16LengthPrefixed(&extData) { return nil, errors.New("bad extension") } switch extension { case 0: // server name var nameList cryptobyte.String if !extData.ReadUint16LengthPrefixed(&nameList) || nameList.Empty() { return nil, errors.New("bad name list") } for !nameList.Empty() { var nameType uint8 var serverName cryptobyte.String if !nameList.ReadUint8(&nameType) || !nameList.ReadUint16LengthPrefixed(&serverName) || serverName.Empty() { return nil, errors.New("bad entry in name list") } if nameType != 0 { continue } if info.ServerName != "" { return nil, errors.New("multiple server names") } info.ServerName = string(serverName) if strings.HasSuffix(info.ServerName, ".") { return nil, errors.New("server name ends with dot") } } case 16: // ALPN var protoList cryptobyte.String if !extData.ReadUint16LengthPrefixed(&protoList) || protoList.Empty() { return nil, errors.New("bad ALPN protocol list") } for !protoList.Empty() { var proto cryptobyte.String if !protoList.ReadUint8LengthPrefixed(&proto) || proto.Empty() { return nil, errors.New("bad ALPN protocol list entry") } info.SupportedProtos = append(info.SupportedProtos, string(proto)) } default: // ignore continue } if !extData.Empty() { return nil, errors.New("extra data at end of extension") } } return &info, nil } func (c *config)
addTrustedRoots
identifier_name
tls.go
session.ClientIP = client } obsoleteVersion := false invalidSSL := false // Read the client hello so that we can find out the name of the server (not // just the address). clientHello, err := readClientHello(conn) if err != nil { logTLS(user, serverAddr, "", fmt.Errorf("error reading client hello: %v", err), false, "") if _, ok := err.(net.Error); ok { conn.Close() return } else if err == ErrObsoleteSSLVersion { obsoleteVersion = true if getConfig().BlockObsoleteSSL { conn.Close() return } } else if err == ErrInvalidSSL { invalidSSL = true } else { conn.Close() return } } clientHelloInfo, err := parseClientHello(clientHello) host, port, err := net.SplitHostPort(serverAddr) if err != nil { host = serverAddr port = "443" } serverName := "" if !obsoleteVersion && !invalidSSL { if clientHelloInfo != nil && clientHelloInfo.ServerName != "" { serverName = clientHelloInfo.ServerName } } session.SNI = serverName if session.ServerAddr == "" { session.ServerAddr = net.JoinHostPort(serverName, "443") } if serverName == "" { serverName = host if ip := net.ParseIP(serverName); ip != nil { // All we have is an IP address, not a name from a CONNECT request. // See if we can do better by reverse DNS. names, err := net.LookupAddr(serverName) if err == nil && len(names) > 0 { serverName = strings.TrimSuffix(names[0], ".") } } } if serverName == "" { logTLS(user, "", "", errors.New("no SNI available"), false, "") conn.Close() return } // Filter a virtual CONNECT request. cr := &http.Request{ Method: "CONNECT", Header: make(http.Header), Host: net.JoinHostPort(serverName, port), URL: &url.URL{Host: serverName}, RemoteAddr: conn.RemoteAddr().String(), } var tlsFingerprint string j, err := ja3.ComputeJA3FromSegment(clientHello) if err != nil { log.Printf("Error generating TLS fingerprint: %v", err) } else { tlsFingerprint = j.GetJA3Hash() ctx := cr.Context() ctx = context.WithValue(ctx, tlsFingerprintKey{}, tlsFingerprint) cr = cr.WithContext(ctx) } var tally map[rule]int var scores map[string]int var reqACLs map[string]bool { conf := getConfig() tally = conf.URLRules.MatchingRules(cr.URL) scores = conf.categoryScores(tally) reqACLs = conf.ACLs.requestACLs(cr, authUser) if invalidSSL { reqACLs["invalid-ssl"] = true } if r == nil { // It's a transparently-intercepted request instead of a real // CONNECT request. reqACLs["transparent"] = true } } session.ACLs.data = reqACLs session.Scores.data = scores session.PossibleActions = []string{"allow", "block"} if getConfig().TLSReady && !obsoleteVersion && !invalidSSL { session.PossibleActions = append(session.PossibleActions, "ssl-bump") } callStarlarkFunctions("ssl_bump", session) dialer := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, DualStack: true, } if session.SourceIP != nil { dialer.LocalAddr = &net.TCPAddr{ IP: session.SourceIP, } } session.chooseAction() logAccess(cr, nil, 0, false, user, tally, scores, session.Action, "", session.Ignored, nil) switch session.Action.Action { case "allow", "": upload, download := connectDirect(conn, session.ServerAddr, clientHello, dialer) logAccess(cr, nil, upload+download, false, user, tally, scores, session.Action, "", session.Ignored, nil) return case "block": conn.Close() return } var cert tls.Certificate var rt http.RoundTripper var http2Support bool closeChan := make(chan struct{}) server := &http.Server{ IdleTimeout: getConfig().CloseIdleConnections, ConnState: func(conn net.Conn, state http.ConnState) { switch state { case http.StateClosed: close(closeChan) } }, } serverConnConfig := &tls.Config{ ServerName: session.SNI, InsecureSkipVerify: true, } clientSupportsHTTP2 := false if clientHelloInfo != nil { for _, p := range clientHelloInfo.SupportedProtos { if p == "h2" { clientSupportsHTTP2 = true } } } if clientSupportsHTTP2 && getConfig().HTTP2Upstream { serverConnConfig.NextProtos = []string{"h2", "http/1.1"} } serverConn, err := tls.DialWithDialer(dialer, "tcp", session.ServerAddr, serverConnConfig) if err == nil { defer serverConn.Close() state := serverConn.ConnectionState() serverCert := state.PeerCertificates[0] valid := validCert(serverCert, state.PeerCertificates[1:]) cert, err = imitateCertificate(serverCert, !valid, session.SNI) if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error generating certificate: %v", err), false, tlsFingerprint) connectDirect(conn, session.ServerAddr, clientHello, dialer) return } http2Support = state.NegotiatedProtocol == "h2" && state.NegotiatedProtocolIsMutual d := &tls.Dialer{ NetDialer: dialer, Config: &tls.Config{ ServerName: session.SNI, RootCAs: certPoolWith(serverConn.ConnectionState().PeerCertificates), }, } if !valid { d.Config.InsecureSkipVerify = true originalCert := serverConn.ConnectionState().PeerCertificates[0] d.Config.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error { cert, err := x509.ParseCertificate(rawCerts[0]) if err != nil { return err } if cert.Equal(originalCert) { return nil } return errCertMismatch } } if http2Support { d.Config.NextProtos = []string{"h2"} var once sync.Once rt = &http2.Transport{ DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { var c net.Conn once.Do(func() { c = serverConn }) if c != nil { return c, nil } logVerbose("redial", "Redialing HTTP/2 connection to %s (%s)", session.SNI, session.ServerAddr) return d.Dial("tcp", session.ServerAddr) }, TLSClientConfig: d.Config, StrictMaxConcurrentStreams: true, } } else { rt = &connTransport{ Conn: serverConn, Redial: func(ctx context.Context) (net.Conn, error) { logVerbose("redial", "Redialing connection to %s (%s)", session.SNI, session.ServerAddr) return d.DialContext(ctx, "tcp", session.ServerAddr) }, } } } else { cert, err = fakeCertificate(session.SNI) if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error generating certificate: %v", err), false, tlsFingerprint) conn.Close() return } rt = httpTransport } session.Freeze() server.Handler = &proxyHandler{ TLS: true, tlsFingerprint: tlsFingerprint, connectPort: port, user: authUser, rt: rt, session: session, } tlsConfig := &tls.Config{ Certificates: []tls.Certificate{cert, getConfig().TLSCert}, PreferServerCipherSuites: true, CurvePreferences: []tls.CurveID{ tls.CurveP256, tls.X25519, // Go 1.8 only }, } http2Downstream := getConfig().HTTP2Downstream && http2Support if http2Downstream { tlsConfig.NextProtos = []string
client := conn.RemoteAddr().String() if host, _, err := net.SplitHostPort(client); err == nil { session.ClientIP = host } else {
random_line_split
tls.go
s = conf.ACLs.requestACLs(cr, authUser) if invalidSSL { reqACLs["invalid-ssl"] = true } if r == nil { // It's a transparently-intercepted request instead of a real // CONNECT request. reqACLs["transparent"] = true } } session.ACLs.data = reqACLs session.Scores.data = scores session.PossibleActions = []string{"allow", "block"} if getConfig().TLSReady && !obsoleteVersion && !invalidSSL { session.PossibleActions = append(session.PossibleActions, "ssl-bump") } callStarlarkFunctions("ssl_bump", session) dialer := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, DualStack: true, } if session.SourceIP != nil { dialer.LocalAddr = &net.TCPAddr{ IP: session.SourceIP, } } session.chooseAction() logAccess(cr, nil, 0, false, user, tally, scores, session.Action, "", session.Ignored, nil) switch session.Action.Action { case "allow", "": upload, download := connectDirect(conn, session.ServerAddr, clientHello, dialer) logAccess(cr, nil, upload+download, false, user, tally, scores, session.Action, "", session.Ignored, nil) return case "block": conn.Close() return } var cert tls.Certificate var rt http.RoundTripper var http2Support bool closeChan := make(chan struct{}) server := &http.Server{ IdleTimeout: getConfig().CloseIdleConnections, ConnState: func(conn net.Conn, state http.ConnState) { switch state { case http.StateClosed: close(closeChan) } }, } serverConnConfig := &tls.Config{ ServerName: session.SNI, InsecureSkipVerify: true, } clientSupportsHTTP2 := false if clientHelloInfo != nil { for _, p := range clientHelloInfo.SupportedProtos { if p == "h2" { clientSupportsHTTP2 = true } } } if clientSupportsHTTP2 && getConfig().HTTP2Upstream { serverConnConfig.NextProtos = []string{"h2", "http/1.1"} } serverConn, err := tls.DialWithDialer(dialer, "tcp", session.ServerAddr, serverConnConfig) if err == nil { defer serverConn.Close() state := serverConn.ConnectionState() serverCert := state.PeerCertificates[0] valid := validCert(serverCert, state.PeerCertificates[1:]) cert, err = imitateCertificate(serverCert, !valid, session.SNI) if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error generating certificate: %v", err), false, tlsFingerprint) connectDirect(conn, session.ServerAddr, clientHello, dialer) return } http2Support = state.NegotiatedProtocol == "h2" && state.NegotiatedProtocolIsMutual d := &tls.Dialer{ NetDialer: dialer, Config: &tls.Config{ ServerName: session.SNI, RootCAs: certPoolWith(serverConn.ConnectionState().PeerCertificates), }, } if !valid { d.Config.InsecureSkipVerify = true originalCert := serverConn.ConnectionState().PeerCertificates[0] d.Config.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error { cert, err := x509.ParseCertificate(rawCerts[0]) if err != nil { return err } if cert.Equal(originalCert) { return nil } return errCertMismatch } } if http2Support { d.Config.NextProtos = []string{"h2"} var once sync.Once rt = &http2.Transport{ DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { var c net.Conn once.Do(func() { c = serverConn }) if c != nil { return c, nil } logVerbose("redial", "Redialing HTTP/2 connection to %s (%s)", session.SNI, session.ServerAddr) return d.Dial("tcp", session.ServerAddr) }, TLSClientConfig: d.Config, StrictMaxConcurrentStreams: true, } } else { rt = &connTransport{ Conn: serverConn, Redial: func(ctx context.Context) (net.Conn, error) { logVerbose("redial", "Redialing connection to %s (%s)", session.SNI, session.ServerAddr) return d.DialContext(ctx, "tcp", session.ServerAddr) }, } } } else { cert, err = fakeCertificate(session.SNI) if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error generating certificate: %v", err), false, tlsFingerprint) conn.Close() return } rt = httpTransport } session.Freeze() server.Handler = &proxyHandler{ TLS: true, tlsFingerprint: tlsFingerprint, connectPort: port, user: authUser, rt: rt, session: session, } tlsConfig := &tls.Config{ Certificates: []tls.Certificate{cert, getConfig().TLSCert}, PreferServerCipherSuites: true, CurvePreferences: []tls.CurveID{ tls.CurveP256, tls.X25519, // Go 1.8 only }, } http2Downstream := getConfig().HTTP2Downstream && http2Support if http2Downstream { tlsConfig.NextProtos = []string{"h2", "http/1.1"} } tlsConn := tls.Server(&insertingConn{conn, clientHello}, tlsConfig) err = tlsConn.Handshake() if err != nil { logTLS(user, session.ServerAddr, serverName, fmt.Errorf("error in handshake with client: %v", err), false, tlsFingerprint) conn.Close() return } logTLS(user, session.ServerAddr, serverName, nil, false, tlsFingerprint) if http2Downstream { http2.ConfigureServer(server, nil) } listener := &singleListener{conn: tlsConn} server.Serve(listener) // Wait for the connection to finish. <-closeChan } // A TLSSession is the parameter for the Starlark ssl_bump function. type TLSSession struct { SNI string ServerAddr string User string ClientIP string // SourceIP is the IP address of the network interface to be used fo dial // the upstream connection. SourceIP net.IP // ConnectHeader is the header from the CONNECT request, if any. ConnectHeader http.Header scoresAndACLs frozen bool misc SyncDict } type scoresAndACLs struct { ACLs StringSet Scores StringIntDict Tally map[rule]int PossibleActions []string Action ACLActionRule Ignored []string } func (s *scoresAndACLs) currentAction() (ar ACLActionRule, ignored []string) { if s.Action.Action != "" { return s.Action, s.Ignored } conf := getConfig() ar, ignored = conf.ChooseACLCategoryAction(s.ACLs.data, s.Scores.data, conf.Threshold, s.PossibleActions...) if ar.Action == "" { ar.Action = "allow" } return ar, ignored } func (s *scoresAndACLs) chooseAction() { s.Action, s.Ignored = s.currentAction() } func (s *scoresAndACLs) setAction(newAction string) error { for _, a := range s.PossibleActions { if newAction == a { s.Action = ACLActionRule{ Action: newAction, Needed: []string{"starlark"}, } return nil } } return fmt.Errorf("can't set action to %q; expected one of %q", newAction, s.PossibleActions) } func (s *TLSSession) String() string { return fmt.Sprintf("TLSSession(%q, %q)", s.SNI, s.ServerAddr) } func (s *TLSSession) Type() string { return "TLSSession" } func (s *TLSSession) Freeze() { if !s.frozen { s.frozen = true s.ACLs.Freeze() s.Scores.Freeze() } } func (s *TLSSession) Truth() starlark.Bool { return starlark.True } func (s *TLSSession) Hash() (uint32, error)
{ return 0, errors.New("unhashable type: TLSSession") }
identifier_body
kek.py
s = pd.read_csv('spam.csv', encoding = 'latin-1') oldmails.head() mailz = pd.read_csv('messages.csv', encoding = 'latin-1') mailz.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis = 1, inplace = True) oldmails.head() mailz.drop(['subject'], axis = 1, inplace = True) mailz.head() #Преобразовани таблицы с данными, переименование столбцов oldmails.rename(columns = {'v1': 'labels', 'v2': 'message'}, inplace = True) oldmails.head() oldmails['labels'].value_counts() mailz['label'].value_counts() #Преобразовани таблицы с данными, переименование значений столбцов oldmails['label'] = oldmails['labels'].map({'ham': 0, 'spam': 1}) oldmails.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['labels'], axis = 1, inplace = True) oldmails.head() #Преобразовани таблицы с данными, слияние двух массивов для обучения mails = pd.concat((mailz, oldmails), ignore_index=True) #Разбиение данных на два массива totalMails = (int(len(mails))-1) trainIndex, testIndex = list(), list() for i in range(mails.shape[0]): if np.random.uniform(0, 1) < 0.75: trainIndex += [i] else: testIndex += [i] trainData = mails.loc[trainIndex] testData = mails.loc[testIndex] #Отображение данных в таблице trainData.reset_index(inplace = True) trainData.drop(['index'], axis = 1, inplace = True) trainData.head() testData.reset_index(inplace = True) testData.drop(['index'], axis = 1, inplace = True) testData.head() #Отображение набора тренировочных данных trainData['label'].value_counts() #Отображение набора данных для тестирования testData['label'].value_coun
ие словрей спам и не спам слов spam_words = ' '.join(list(mails[mails['label'] == 1]['message'])) ham_words = ' '.join(list(mails[mails['label'] == 0]['message'])) trainData.head() trainData['label'].value_counts() testData.head() testData['label'].value_counts() #Обработка текста сообщений def process_message(message, lower_case = True, stem = True, stop_words = True, gram = 2): if lower_case: message = message.lower() words = word_tokenize(message) words = [w for w in words if len(w) > 2] if gram > 1: w = [] for i in range(len(words) - gram + 1): w += [' '.join(words[i:i + gram])] return w if stop_words: sw = stopwords.words('english') words = [word for word in words if word not in sw] if stem: stemmer = PorterStemmer() words = [stemmer.stem(word) for word in words] print(words) return words #Классификация данных class SpamClassifier(object): def __init__(self, trainData, method='tf-idf'): self.mails, self.labels = trainData['message'], trainData['label'] self.method = method #Функция обучения def train(self): self.calc_TF_and_IDF() if self.method == 'tf-idf': self.calc_TF_IDF() else: self.calc_prob() def calc_prob(self): self.prob_spam = dict() self.prob_ham = dict() for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word] + 1) / (self.spam_words + \ len(list(self.tf_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word] + 1) / (self.ham_words + \ len(list(self.tf_ham.keys()))) self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Вычисление вероятностей def calc_TF_and_IDF(self): noOfMessages = self.mails.shape[0] self.spam_mails, self.ham_mails = self.labels.value_counts()[1], self.labels.value_counts()[0] self.total_mails = self.spam_mails + self.ham_mails self.spam_words = 0 self.ham_words = 0 self.tf_spam = dict() self.tf_ham = dict() self.idf_spam = dict() self.idf_ham = dict() for i in range(noOfMessages): message_processed = process_message(self.mails[i]) count = list() for word in message_processed: if self.labels[i]: self.tf_spam[word] = self.tf_spam.get(word, 0) + 1 self.spam_words += 1 else: self.tf_ham[word] = self.tf_ham.get(word, 0) + 1 self.ham_words += 1 if word not in count: count += [word] for word in count: if self.labels[i]: self.idf_spam[word] = self.idf_spam.get(word, 0) + 1 else: self.idf_ham[word] = self.idf_ham.get(word, 0) + 1 def calc_TF_IDF(self): self.prob_spam = dict() self.prob_ham = dict() self.sum_tf_idf_spam = 0 self.sum_tf_idf_ham = 0 for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam[word] + self.idf_ham.get(word, 0))) self.sum_tf_idf_spam += self.prob_spam[word] for word in self.tf_spam: self.prob_spam[word] = (self.prob_spam[word] + 1) / ( self.sum_tf_idf_spam + len(list(self.prob_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam.get(word, 0) + self.idf_ham[word])) self.sum_tf_idf_ham += self.prob_ham[word] for word in self.tf_ham: self.prob_ham[word] = (self.prob_ham[word] + 1) / (self.sum_tf_idf_ham + len(list(self.prob_ham.keys()))) self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Непосредственно функция классификации на основе теоремы Байеса def classify(self, processed_message): pSpam, pHam = 0, 0 for word in processed_message: if word in self.prob_spam: pSpam += log(self.prob_spam[word]) else: if self.method == 'tf-idf': pSpam -= log(self.sum_tf_idf_spam + len(list(self.prob_spam.keys()))) else: pSpam -= log(self.spam_words + len(list(self.prob_spam.keys()))) if word in self.prob_ham: pHam += log(self.prob_ham[word]) else: if self.method == 'tf-idf': pHam -= log(self.sum_tf_idf_ham + len(list(self.prob_ham.keys()))) else: pHam -= log(self.ham_words + len(list(self.prob_ham.keys()))) pSpam += log(self.prob_spam_mail) pHam += log(self.prob_ham_mail) return pSpam >= pHam #Функция предсказания является ли сообщение спамом или нет def predict(self, testData): result = dict() for (i, message) in enumerate(testData): processed_message = process_message(message) result[i] = int(self.classify(processed_message)) return result #Функция вычисления качества работы алгоритма def metrics(labels, predictions): true_pos, true_neg, false_pos, false_neg = 0, 0, 0, 0 for i in range(len(labels)): true_pos += int(labels[i] == 1 and predictions[i] == 1) true_neg += int(labels[i] == 0 and predictions[i] == 0) false_pos += int(labels[i] ==
ts() #Формирован
conditional_block
kek.py
ate(spam_words) plt.figure(figsize = (10, 8), facecolor = 'k') plt.imshow(spam_wc) plt.axis('off') plt.tight_layout(pad = 0) plt.show() #Функция визуализации словаря легетимных слов def show_ham(ham_words): ham_wc = WordCloud(width = 512,height = 512).generate(ham_words) plt.figure(figsize = (10, 8), facecolor = 'k') plt.imshow(ham_wc) plt.axis('off') plt.tight_layout(pad = 0) plt.show() #Чтение данных из таблицы oldmails = pd.read_csv('spam.csv', encoding = 'latin-1') oldmails.head() mailz = pd.read_csv('messages.csv', encoding = 'latin-1') mailz.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis = 1, inplace = True) oldmails.head() mailz.drop(['subject'], axis = 1, inplace = True) mailz.head() #Преобразовани таблицы с данными, переименование столбцов oldmails.rename(columns = {'v1': 'labels', 'v2': 'message'}, inplace = True) oldmails.head() oldmails['labels'].value_counts() mailz['label'].value_counts() #Преобразовани таблицы с данными, переименование значений столбцов oldmails['label'] = oldmails['labels'].map({'ham': 0, 'spam': 1}) oldmails.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['labels'], axis = 1, inplace = True) oldmails.head() #Преобразовани таблицы с данными, слияние двух массивов для обучения mails = pd.concat((mailz, oldmails), ignore_index=True) #Разбиение данных на два массива totalMails = (int(len(mails))-1) trainIndex, testIndex = list(), list() for i in range(mails.shape[0]): if np.random.uniform(0, 1) < 0.75: trainIndex += [i] else: testIndex += [i] trainData = mails.loc[trainIndex] testData = mails.loc[testIndex] #Отображение данных в таблице trainData.reset_index(inplace = True) trainData.drop(['index'], axis = 1, inplace = True) trainData.head() testData.reset_index(inplace = True) testData.drop(['index'], axis = 1, inplace = True) testData.head() #Отображение набора тренировочных данных trainData['label'].value_counts() #Отображение набора данных для тестирования testData['label'].value_counts() #Формирование словрей спам и не спам слов spam_words = ' '.join(list(mails[mails['label'] == 1]['message'])) ham_words = ' '.join(list(mails[mails['label'] == 0]['message'])) trainData.head() trainData['label'].value_counts() testData.head() testData['label'].value_counts() #Обработка текста сообщений def process_message(message, lower_case = True, stem = True, stop_words = True, gram = 2): if lower_case: message = message.lower() words = word_tokenize(message) words = [w for w in words if len(w) > 2] if gram > 1: w = [] for i in range(len(words) - gram + 1): w += [' '.join(words[i:i + gram])] return w if stop_words: sw = stopwords.words('english') words = [word for word in words if word not in sw] if stem: stemmer = PorterStemmer() words = [stemmer.stem(word) for word in words] print(words) return words #Классификация данных class SpamClassifier(object): def __init__(self, trainData, method='tf-idf'): self.mails, self.labels = trainData['message'], trainData['label'] self.method = method #Функция обучения def train(self): self.calc_TF_and_IDF() if self.method == 'tf-idf': self.calc_TF_IDF() else: self.calc_prob() def calc_prob(self): self.prob_spam = dict() self.prob_ham = dict() for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word] + 1) / (self.spam_words + \ len(list(self.tf_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word] + 1) / (self.ham_words + \ len(list(self.tf_ham.keys()))) self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Вычисление вероятностей def calc_TF_and_IDF(self): noOfMessages = self.mails.shape[0] self.spam_mails, self.ham_mails = self.labels.value_counts()[1], self.labels.value_counts()[0] self.total_mails = self.spam_mails + self.ham_mails self.spam_words = 0 self.ham_words = 0 self.tf_spam = dict() self.tf_ham = dict() self.idf_spam = dict() self.idf_ham = dict() for i in range(noOfMessages): message_processed = process_message(self.mails[i]) count = list() for word in message_processed: if self.labels[i]: self.tf_spam[word] = self.tf_spam.get(word, 0) + 1 self.spam_words += 1 else: self.tf_ham[word] = self.tf_ham.get(word, 0) + 1 self.ham_words += 1 if word not in count: count += [word] for word in count: if self.labels[i]: self.idf_spam[word] = self.idf_spam.get(word, 0) + 1 else: self.idf_ham[word] = self.idf_ham.get(word, 0) + 1 def calc_TF_IDF(self): self.prob_spam = dict() self.prob_ham = dict() self.sum_tf_idf_spam = 0 self.sum_tf_idf_ham = 0 for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam[word] + self.idf_ham.get(word, 0))) self.sum_tf_idf_spam += self.prob_spam[word] for word in self.tf_spam: self.prob_spam[word] = (self.prob_spam[word] + 1) / ( self.sum_tf_idf_spam + len(list(self.prob_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam.get(word, 0) + self.idf_ham[word])) self.sum_tf_idf_ham += self.prob_ham[word] for word in self.tf_ham: self.prob_ham[word] = (self.prob_ham[word] + 1) / (self.sum_tf_idf_ham + len(list(self.prob_ham.keys()))) self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Непосредственно функция классификации на основе теоремы Байеса def classify(self, processed_message): pSpam, pHam = 0, 0 for word in processed_message: if word in self.prob_spam: pSpam += log(self.prob_spam[word]) else: if self.method == 'tf-idf': pSpam -= log(self.sum_tf_idf_spam + len(list(self.prob_spam.keys()))) else: pSpam -= log(self.spam_words + len(list(self.prob_spam.keys()))) if word in self.prob_ham: pHam += log(self.prob_ham[word]) else: if self.method == 'tf-idf': pHam -= log(self.sum_tf_idf_ham + len(list(self.prob_ham.keys()))) else: pHam -= log(self.ham_words + len(list(self.prob_ham.keys()))) pSpam += log(self.prob_spam_mail) pHam += log(self.prob
слов def show_spam(spam_words): spam_wc = WordCloud(width = 512,height = 512).gener
identifier_body
kek.py
mails = pd.read_csv('spam.csv', encoding = 'latin-1') oldmails.head() mailz = pd.read_csv('messages.csv', encoding = 'latin-1') mailz.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis = 1, inplace = True) oldmails.head() mailz.drop(['subject'], axis = 1, inplace = True) mailz.head() #Преобразовани таблицы с данными, переименование столбцов oldmails.rename(columns = {'v1': 'labels', 'v2': 'message'}, inplace = True) oldmails.head() oldmails['labels'].value_counts() mailz['label'].value_counts() #Преобразовани таблицы с данными, переименование значений столбцов oldmails['label'] = oldmails['labels'].map({'ham': 0, 'spam': 1}) oldmails.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['labels'], axis = 1, inplace = True) oldmails.head() #Преобразовани таблицы с данными, слияние двух массивов для обучения mails = pd.concat((mailz, oldmails), ignore_index=True) #Разбиение данных на два массива totalMails = (int(len(mails))-1) trainIndex, testIndex = list(), list() for i in range(mails.shape[0]): if np.random.uniform(0, 1) < 0.75: trainIndex += [i] else: testIndex += [i] trainData = mails.loc[trainIndex] testData = mails.loc[testIndex] #Отображение данных в таблице trainData.reset_index(inplace = True) trainData.drop(['index'], axis = 1, inplace = True) trainData.head() testData.reset_index(inplace = True) testData.drop(['index'], axis = 1, inplace = True) testData.head() #Отображение набора тренировочных данных trainData['label'].value_counts() #Отображение набора данных для тестирования testData['label'].value_counts() #Формирование словрей спам и не спам слов spam_words = ' '.join(list(mails[mails['label'] == 1]['message'])) ham_words = ' '.join(list(mails[mails['label'] == 0]['message'])) trainData.head() trainData['label'].value_counts() testData.head() testData['label'].value_counts() #Обработка текста сообщений def process_message(message, lower_case = True, stem = True, stop_words = True, gram = 2): if lower_case: message = message.lower() words = word_tokenize(message) words = [w for w in words if len(w) > 2] if gram > 1: w = [] for i in range(len(words) - gram + 1): w += [' '.join(words[i:i + gram])] return w if stop_words: sw = stopwords.words('english') words = [word for word in words if word not in sw] if stem: stemmer = PorterStemmer() words = [stemmer.stem(word) for word in words] print(words) return words #Классификация данных class SpamClassifier(object): def __init__(self, trainData, method='tf-idf'): self.mails, self.labels = trainData['message'], trainData['label'] self.method = method #Функция обучения def train(self): self.calc_TF_and_IDF() if self.method == 'tf-idf': self.calc_TF_IDF() else: self.calc_prob() def calc_prob(self): self.prob_spam = dict() self.prob_ham = dict() for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word] + 1) / (self.spam_words + \ len(list(self.tf_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word] + 1) / (self.ham_words + \ len(list(self.tf_ham.keys()))) self.prob_spam_mail, self.prob_ham
= self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Вычисление вероятностей def calc_TF_and_IDF(self): noOfMessages = self.mails.shape[0] self.spam_mails, self.ham_mails = self.labels.value_counts()[1], self.labels.value_counts()[0] self.total_mails = self.spam_mails + self.ham_mails self.spam_words = 0 self.ham_words = 0 self.tf_spam = dict() self.tf_ham = dict() self.idf_spam = dict() self.idf_ham = dict() for i in range(noOfMessages): message_processed = process_message(self.mails[i]) count = list() for word in message_processed: if self.labels[i]: self.tf_spam[word] = self.tf_spam.get(word, 0) + 1 self.spam_words += 1 else: self.tf_ham[word] = self.tf_ham.get(word, 0) + 1 self.ham_words += 1 if word not in count: count += [word] for word in count: if self.labels[i]: self.idf_spam[word] = self.idf_spam.get(word, 0) + 1 else: self.idf_ham[word] = self.idf_ham.get(word, 0) + 1 def calc_TF_IDF(self): self.prob_spam = dict() self.prob_ham = dict() self.sum_tf_idf_spam = 0 self.sum_tf_idf_ham = 0 for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam[word] + self.idf_ham.get(word, 0))) self.sum_tf_idf_spam += self.prob_spam[word] for word in self.tf_spam: self.prob_spam[word] = (self.prob_spam[word] + 1) / ( self.sum_tf_idf_spam + len(list(self.prob_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam.get(word, 0) + self.idf_ham[word])) self.sum_tf_idf_ham += self.prob_ham[word] for word in self.tf_ham: self.prob_ham[word] = (self.prob_ham[word] + 1) / (self.sum_tf_idf_ham + len(list(self.prob_ham.keys()))) self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Непосредственно функция классификации на основе теоремы Байеса def classify(self, processed_message): pSpam, pHam = 0, 0 for word in processed_message: if word in self.prob_spam: pSpam += log(self.prob_spam[word]) else: if self.method == 'tf-idf': pSpam -= log(self.sum_tf_idf_spam + len(list(self.prob_spam.keys()))) else: pSpam -= log(self.spam_words + len(list(self.prob_spam.keys()))) if word in self.prob_ham: pHam += log(self.prob_ham[word]) else: if self.method == 'tf-idf': pHam -= log(self.sum_tf_idf_ham + len(list(self.prob_ham.keys()))) else: pHam -= log(self.ham_words + len(list(self.prob_ham.keys()))) pSpam += log(self.prob_spam_mail) pHam += log(self.prob_ham_mail) return pSpam >= pHam #Функция предсказания является ли сообщение спамом или нет def predict(self, testData): result = dict() for (i, message) in enumerate(testData): processed_message = process_message(message) result[i] = int(self.classify(processed_message)) return result #Функция вычисления качества работы алгоритма def metrics(labels, predictions): true_pos, true_neg, false_pos, false_neg = 0, 0, 0, 0 for i in range(len(labels)): true_pos += int(labels[i] == 1 and predictions[i] == 1) true_neg += int(labels[i] == 0 and predictions[i] == 0) false_pos += int(labels[i] == 0
_mail
identifier_name
kek.py
from sklearn.feature_extraction.text import TfidfTransformer from sklearn.naive_bayes import MultinomialNB from sklearn.metrics import confusion_matrix import pickle #Функция сохранения состояния обученности классификатора def save(obj): with open('sis.pickle', 'wb') as f: pickle.dump(obj, f) #Функция загрузки состояния обученности классификатора def load(): with open('sis.pickle', 'rb') as f: obj_new = pickle.load(f) return obj_new #Функция визуализации словаря спам слов def show_spam(spam_words): spam_wc = WordCloud(width = 512,height = 512).generate(spam_words) plt.figure(figsize = (10, 8), facecolor = 'k') plt.imshow(spam_wc) plt.axis('off') plt.tight_layout(pad = 0) plt.show() #Функция визуализации словаря легетимных слов def show_ham(ham_words): ham_wc = WordCloud(width = 512,height = 512).generate(ham_words) plt.figure(figsize = (10, 8), facecolor = 'k') plt.imshow(ham_wc) plt.axis('off') plt.tight_layout(pad = 0) plt.show() #Чтение данных из таблицы oldmails = pd.read_csv('spam.csv', encoding = 'latin-1') oldmails.head() mailz = pd.read_csv('messages.csv', encoding = 'latin-1') mailz.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis = 1, inplace = True) oldmails.head() mailz.drop(['subject'], axis = 1, inplace = True) mailz.head() #Преобразовани таблицы с данными, переименование столбцов oldmails.rename(columns = {'v1': 'labels', 'v2': 'message'}, inplace = True) oldmails.head() oldmails['labels'].value_counts() mailz['label'].value_counts() #Преобразовани таблицы с данными, переименование значений столбцов oldmails['label'] = oldmails['labels'].map({'ham': 0, 'spam': 1}) oldmails.head() #Преобразовани таблицы с данными, удаление лишних столбцов oldmails.drop(['labels'], axis = 1, inplace = True) oldmails.head() #Преобразовани таблицы с данными, слияние двух массивов для обучения mails = pd.concat((mailz, oldmails), ignore_index=True) #Разбиение данных на два массива totalMails = (int(len(mails))-1) trainIndex, testIndex = list(), list() for i in range(mails.shape[0]): if np.random.uniform(0, 1) < 0.75: trainIndex += [i] else: testIndex += [i] trainData = mails.loc[trainIndex] testData = mails.loc[testIndex] #Отображение данных в таблице trainData.reset_index(inplace = True) trainData.drop(['index'], axis = 1, inplace = True) trainData.head() testData.reset_index(inplace = True) testData.drop(['index'], axis = 1, inplace = True) testData.head() #Отображение набора тренировочных данных trainData['label'].value_counts() #Отображение набора данных для тестирования testData['label'].value_counts() #Формирование словрей спам и не спам слов spam_words = ' '.join(list(mails[mails['label'] == 1]['message'])) ham_words = ' '.join(list(mails[mails['label'] == 0]['message'])) trainData.head() trainData['label'].value_counts() testData.head() testData['label'].value_counts() #Обработка текста сообщений def process_message(message, lower_case = True, stem = True, stop_words = True, gram = 2): if lower_case: message = message.lower() words = word_tokenize(message) words = [w for w in words if len(w) > 2] if gram > 1: w = [] for i in range(len(words) - gram + 1): w += [' '.join(words[i:i + gram])] return w if stop_words: sw = stopwords.words('english') words = [word for word in words if word not in sw] if stem: stemmer = PorterStemmer() words = [stemmer.stem(word) for word in words] print(words) return words #Классификация данных class SpamClassifier(object): def __init__(self, trainData, method='tf-idf'): self.mails, self.labels = trainData['message'], trainData['label'] self.method = method #Функция обучения def train(self): self.calc_TF_and_IDF() if self.method == 'tf-idf': self.calc_TF_IDF() else: self.calc_prob() def calc_prob(self): self.prob_spam = dict() self.prob_ham = dict() for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word] + 1) / (self.spam_words + \ len(list(self.tf_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word] + 1) / (self.ham_words + \ len(list(self.tf_ham.keys()))) self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Вычисление вероятностей def calc_TF_and_IDF(self): noOfMessages = self.mails.shape[0] self.spam_mails, self.ham_mails = self.labels.value_counts()[1], self.labels.value_counts()[0] self.total_mails = self.spam_mails + self.ham_mails self.spam_words = 0 self.ham_words = 0 self.tf_spam = dict() self.tf_ham = dict() self.idf_spam = dict() self.idf_ham = dict() for i in range(noOfMessages): message_processed = process_message(self.mails[i]) count = list() for word in message_processed: if self.labels[i]: self.tf_spam[word] = self.tf_spam.get(word, 0) + 1 self.spam_words += 1 else: self.tf_ham[word] = self.tf_ham.get(word, 0) + 1 self.ham_words += 1 if word not in count: count += [word] for word in count: if self.labels[i]: self.idf_spam[word] = self.idf_spam.get(word, 0) + 1 else: self.idf_ham[word] = self.idf_ham.get(word, 0) + 1 def calc_TF_IDF(self): self.prob_spam = dict() self.prob_ham = dict() self.sum_tf_idf_spam = 0 self.sum_tf_idf_ham = 0 for word in self.tf_spam: self.prob_spam[word] = (self.tf_spam[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam[word] + self.idf_ham.get(word, 0))) self.sum_tf_idf_spam += self.prob_spam[word] for word in self.tf_spam: self.prob_spam[word] = (self.prob_spam[word] + 1) / ( self.sum_tf_idf_spam + len(list(self.prob_spam.keys()))) for word in self.tf_ham: self.prob_ham[word] = (self.tf_ham[word]) * log((self.spam_mails + self.ham_mails) \ / (self.idf_spam.get(word, 0) + self.idf_ham[word])) self.sum_tf_idf_ham += self.prob_ham[word] for word in self.tf_ham: self.prob_ham[word] = (self.prob_ham[word] + 1) / (self.sum_tf_idf_ham + len(list(self.prob_ham.keys()))) self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails #Непосредственно функция классификации на основе теоремы Байеса def classify(self, processed_message): pSpam, pHam = 0, 0 for word in processed_message: if word in self.prob
import numpy as np import pandas as pd import nltk from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import CountVectorizer
random_line_split