Exemplo n.º 1
0
    def printPath(self, path):
        statePath = []
        for i in range(len(path)):
            # print(path[i].getState().pprint_string())
            statePath.append(path[i].getState())

        util.pprint(statePath)
Exemplo n.º 2
0
def roll(dice_spec: str) -> Tuple[int, Sequence[int]]:
    tracer = execution_context.get_opencensus_tracer()
    try:
        with tracer.span('initial_parse'):
            tracer.add_attribute_to_current_span("dice_spec", dice_spec)
            tree = get_parser().parse(dice_spec)
    except LarkError as e:
        raise RecognitionError(
            "Sorry, I couldn't understand your request") from e
    logging.debug("Initial parse tree:\n%s", pprint(tree))
    try:
        tree = NumberTransformer().transform(tree)
        with tracer.span('dnd_knowledge'):
            tree = DnD5eKnowledge().transform(tree)
        tree = SimplifyTransformer().transform(tree)
        with tracer.span('crit_transform'):
            tree = CritTransformer().transform(tree)
        logging.debug("DnD transformed parse tree:\n%s", pprint(tree))
        with tracer.span('final_eval'):
            transformer = EvalDice()
            tree = transformer.transform(tree)
        tree = SimplifyTransformer().transform(tree)
    except VisitError as e:
        #  Get our nice exception out of lark's wrapper
        raise e.orig_exc
    return (tree.children[0], transformer.dice_results)
Exemplo n.º 3
0
 def connect(self):
     self.sock = socket.create_connection(
             (self.irc_config['server'], self.irc_config['port']))
     self.fp = self.sock.makefile('rw', 0)
     pprint('Connected to %s:%d, registering as %s!%s (%s)' % 
             (self.irc_config['server'], self.irc_config['port'], 
             self.irc_config['nick'], self.irc_config['user'], self.irc_config['name']))
     self.change_nick(self.irc_config['nick'])
     self.send('USER %s 8 * :%s' % (self.irc_config['user'], self.irc_config['name']))
Exemplo n.º 4
0
def upload_dir(local, remote, dry_run=False, opts=None):
    env = environment()
    if 'ssh' in env:
        ssh = env['ssh']
        rsync_opts = [
            '--recursive', '--archive', '--compress', '--itemize-changes'
        ]
        if dry_run:
            rsync_opts.append('--dry-run')
        if state['verbose']:
            rsync_opts.append('--verbose')
        # trailing slash is important, see rsync documentation
        src = local + '/'
        abs_remote = os.path.join(ssh['document_root'], remote)
        dest = '{}@{}:{}'.format(ssh['user'], ssh['host'], abs_remote)
        args = rsync_opts + ([] if opts is None else opts) + [src, dest]
        # TODO rsync still prompts for the password
        with shell_env(RSYNC_PASSWORD=ssh['password']):
            fab.local('rsync {}'.format(' '.join(args)))
    else:
        ftp = env['ftp']
        host = ftp_host(env)
        extra_opts = {'ftp_debug': 1 if state['verbose'] else 0}
        url = urlparse(ftp['url'])
        if url.scheme != 'ftp':
            fab.warn('non-ftp url scheme, may not be supported')
        abs_remote = os.path.join(url.path, remote)
        if not host.path.exists(abs_remote):
            fab.warn('remote path is missing: {}'.format(abs_remote))
            if console.confirm('create missing directories?'):
                host.makedirs(abs_remote)
        local_target = FsTarget(local, extra_opts=extra_opts)
        remote_target = FtpTarget(path=abs_remote,
                                  host=url.netloc,
                                  username=ftp['user'],
                                  password=ftp['password'],
                                  extra_opts=extra_opts)
        opts = {
            'force': False,
            # TODO
            'delete_unmatched': False,
            'verbose': 3,
            'execute': True,
            'dry_run': dry_run
        }
        s = UploadSynchronizer(local_target, remote_target, opts)
        try:
            s.run()
        except KeyboardInterrupt:
            fab.warn('aborted by user')
        finally:
            s.local.close()
            s.remote.close()
        stats = s.get_stats()
        pprint(stats)
Exemplo n.º 5
0
 def dispatch(self):
     while True:
         try:
             l = self.readline()
         except socket.timeout:
             pprint('Socket timeout (server not responding), quitting')
             self.quit(graceful=False) # don't send quit message
         except KeyboardInterrupt:
             self.quit(msg='I was sent SIGINT')
         else:
             self._handle_line(l)
def example1():
    """
    find all customers who never order anything.
    """
    query = ses.query(Customer.name) \
        .filter(
        Customer.id.notin_(
            ses.query(func.distinct(Order.customer_id))
        )
    )
    pprint(query, engine)
Exemplo n.º 7
0
def join(server, req, chan):
    if chan[0] not in ['#', '&']:
        server.send_privmsg(req['nick'], 'Please specify channel name correctly (missing #/& prefix)')
        return
    if chan in server.joined_channels:
        server.send_privmsg(req['nick'], 'I am already joined to %s' % chan)
        return
    if chan in server.pending_channel_joins:
        server.send_privmsg(req['nick'], 'I am already trying to join %s' % chan)
        return
    pprint('Joining channel %s at the request of %s' % (chan, req['nick']))
    server.join_channel(chan, req['nick'])
def permutation1():
    query = text("""
    SELECT 
        E.id as e_id, 
        E.name as e_name, 
        D.id as d_id, 
        D.name as d_name
    FROM 
        employee as E, 
        department as D
    """)
    pprint(query, engine)
def experiment(full_data):
    fake_reviews = [
        review_text for (rid, review_text, auth, sent) in full_data
        if auth == "Fake"
    ]
    true_reviews = [
        review_text for (rid, review_text, auth, sent) in full_data
        if auth == "True"
    ]
    # print(len(fake_reviews))
    # pprint(fake_reviews)
    pprint(true_reviews)
def solution():
    """
    find the cancellation rate of requests made by unbanned users
    between 2017-01-01 to 2017-01-03
    """
    subquery = ses.query(Attendance).filter(Attendance.date.between("2017-01-01", "2017-01-03"))

    query = ses.query(
            ses.query(subquery.filter(Attendance.is_attendance==True)),
        ) \
        .group_by(Attendance.date)

    pprint(query, engine)
Exemplo n.º 11
0
def main():

    if len(sys.argv) < 2:
        print("[ERROR]: Album index not specified")
        sys.exit()

    gphoto.init()
    GoogleAlbums.load_albums()
    cache = GoogleAlbums.cache()
    album_list = cache['list']

    idx = int(sys.argv[1])
    album = album_list[idx]
    util.pprint(album)
Exemplo n.º 12
0
    def testPPrint(self):
        """ Test pretty-printing EBML files. """
        schemaFile = './schemata/mide_ide.xml'
        schema = core.loadSchema(schemaFile)

        ebmlDoc = schema.load('./tests/SSX46714-doesnot.IDE', headers=True)

        util.pprint(ebmlDoc, out=open('./tests/IDE-Pretty.txt', 'wt'))
        xmlString = ET.tostring(util.toXml(ebmlDoc))
        prettyXmlFile = open('./tests/IDE-Pretty.xml', 'wt')
        parseString(xmlString).writexml(prettyXmlFile,
                                        addindent='\t',
                                        newl='\n',
                                        encoding='utf-8')
def permutation2():
    query = text("""
    SELECT 
        E.id as e_id, 
        E.name as e_name, 
        D1.id as d_id1, 
        D1.name as d_name1,
        D2.id as d_id2, 
        D2.name as d_name2
    FROM 
        employee as E, 
        department as D1,
        department as D2
    """)
    pprint(query, engine)
Exemplo n.º 14
0
    def play(self):
        self.gameState = State()

        while not self.gameState.game_over():
            player1Action = self.player1.choose_action(self.gameState)
            self.gameState.execute(player1Action)
            util.pprint(self.gameState)

            if self.gameState.game_over():
                break

            player2Action = self.player2.choose_action(self.gameState)
            self.gameState.execute(player2Action)
            util.pprint(self.gameState)

        return self.gameState.winner()
Exemplo n.º 15
0
 async def watchTicker(self, newOI: asyncio.Event):
     while True:
         try:
             if "watchTicker" in self.ccxt.has:
                 self.ticker = await self.ccxt.watch_ticker(self.market)
             else:
                 await asyncio.sleep(2)
                 await self.fetchTicker()
         except ccxt.base.errors.ExchangeNotAvailable as err:
             pprint("%s: %s" % (self, err))
         except:
             logging.exception("watchTicker unhandled exception")
         oi = self.getOI()
         if oi != self.oi:
             self.oi = oi
             newOI.set()
Exemplo n.º 16
0
def visualise_pattern(data):
    pattern_id = data['pattern_id']
    send('Loading pattern')
    role_pattern = db.load_role_pattern(pattern_id)
    pprint(role_pattern.spacy_dep_pattern)
    send('Generating DOT')
    node_attrs = role_pattern_vis.DEFAULT_NODE_ATTRS
    # for token in doc:
    #     token._.plot.update(node_attrs)
    #     token._.plot['label'] = '{0} [{1}]\n({2})'.format(token.orth_, token.i, token.tag_)
    graph, legend = role_pattern.to_pydot(legend=True)
    graph, legend = graph.to_string(), legend.to_string()
    dot_data = {
        'graph': graph,
        'legend': legend,
    }
    emit('visualise_pattern_success', dot_data)
Exemplo n.º 17
0
    def random_walk(self, state, n):
        currNode = Node(state)

        for _ in range(n - 1):
            nextNode = Node(deepcopy(currNode.state))
            nextNode.setParent(currNode)

            childIndex = random.randint(0,
                                        len(nextNode.possibleChildStates) - 1)
            nextAction = nextNode.possibleChildStates[childIndex]
            nextNode.state.execute(nextAction)

            currNode.addChild(nextNode)
            currNode = nextNode

        states = get_root_traversal_states(currNode)
        util.pprint(states)
def solution1():
    """
    Not perfect solution. If seat id ends with odds number, then it doesn't work.
    """
    query = ses.query(Seating)
    pprint(query, engine)

    query = text("""
    SELECT seating.id - 1 as id, seating.student
    FROM seating
    WHERE seating.id % 2 = 0
    UNION
    SELECT seating.id + 1 as id, seating.student
    FROM seating
    WHERE seating.id % 2 = 1
    ORDER BY id
    """)
    pprint(query, engine)
Exemplo n.º 19
0
    def search(self, isBfs=True):
        goalNode = None
        currentDFSDepth = 0
        iterations = 0

        while self.open:
            node = self.open.pop(0)
            self.closed.append(node)
            if node.state.is_goal():
                goalNode = node
                break

            if self.searchType == 'dfs':
                if currentDFSDepth <= self.maxDfsDepth:
                    currentDFSDepth += 1
                else:
                    currentDFSDepth = 0
                    continue

            iterations += 1
            for action in node.getPossibleActions():
                newNode = deepcopy(node)
                newNode.state.execute(action)

                if newNode not in self.closed or newNode not in self.open:
                    node.addChild(newNode)
                    newNode.setParent(node)
                    if self.searchType == 'bfs':
                        self.open.append(newNode)
                    elif self.searchType == 'dfs':
                        self.open.insert(0, newNode)
                    else:
                        self.open.append(newNode)
                        self.open.sort(
                            key=lambda h: self.heuristic(h.state, h.depth))

        if goalNode:
            states = get_root_traversal_states(goalNode)
            util.pprint(states)
            return iterations
        else:
            print('Search failed, unable to find goal state.')
            return None
Exemplo n.º 20
0
    def _handle_line(self, line):
        if len(line) == 0:
            pprint('Server closed connection, quitting')
            self.quit(graceful=False) # don't send quit message

        parts = line.split(' ')
        if parts[0][0] == ':':
            key = parts[1]
        else:
            key = parts[0]
        
        # special case for numeric messages
        if self._numeric_msg_patt.match(key):
            key = 'numeric_%s' % key

        callback = getattr(server_callbacks, key.lower(), None)
        if callback is not None:
            callback(self, parts, line)
        else:
            pprint(colored.red('Unhandled %s << %s' % (key, line)))
Exemplo n.º 21
0
 def WEAPON(self, name) -> Tree:
     tracer = execution_context.get_opencensus_tracer()
     weapon = self.find_named_object(name, WEAPONS)
     dice_spec = weapon["damage_dice"]
     with tracer.span('parse_weapon'):
         tracer.add_attribute_to_current_span("name", name)
         tracer.add_attribute_to_current_span("dice_spec", dice_spec)
         tree = get_parser().parse(dice_spec, start="sum")
     logging.debug("weapon %s has damage dice %s parsed as:\n%s", name,
                   dice_spec, pprint(tree))
     return tree
def solution1():
    """
    Not perfect solution. If seat id ends with odds number, then it doesn't work.
    """
    query = ses.query(Employee)
    pprint(query, engine)
    query = ses.query(Department)
    pprint(query, engine)

    query = text("""
    SELECT employee.department_id, COUNT(*)
    FROM employee
    GROUP BY employee.department_id
    """)
    query = text("""
    SELECT 
        D.id as department_id, 
        E1.name as employee_name, 
        E1.salary as salary1,
        E2.salary as salary2
    FROM 
        department D, 
        employee E1, 
        employee E2
    """)
    """
    WHERE 
        D.id = E1.department_id 
        AND E1.department_id = E2.department_id 
        AND E1.salary <= E2.salary
    group by D.id, E1.name having count(distinct E2.salary) <= 3
    order by D.name, E1.salary desc
    """
    pprint(query, engine)
def example():
    """
    每个movie都有若干个genre. 我们想要统计对于每个genre, 所有包含这个genre的movie, 平均有多少个genre.
    """
    query = text("""
    SELECT genre.name, result.avg_genre_count
    FROM (
        SELECT movie_and_genre.genre_id, AVG(genre_count_each_movie.genre_count) as avg_genre_count
        FROM movie_and_genre
        JOIN
        (
            SELECT movie_and_genre.movie_id, COUNT(*) as genre_count
            FROM movie_and_genre
            GROUP BY movie_and_genre.movie_id
        ) as genre_count_each_movie
        ON movie_and_genre.movie_id = genre_count_each_movie.movie_id
        GROUP BY movie_and_genre.genre_id
    ) as result
    JOIN genre
    ON result.genre_id = genre.id
    """)
    pprint(query, engine)
Exemplo n.º 24
0
 def spell_default(self, spell: Mapping[str, Any]) -> Tree:
     tracer = execution_context.get_opencensus_tracer()
     m = re.search(r"\d+d\d+( \+ \d+)?", spell["desc"])
     if not m:
         raise ImpossibleSpellError(
             f"Sorry, I couldn't find the damage dice for %s" %
             spell["name"])
     with tracer.span('parse_spell'):
         tracer.add_attribute_to_current_span("name", spell["name"])
         tracer.add_attribute_to_current_span("dice_spec", m.group(0))
         tree = get_parser().parse(m.group(0), start="sum")
     logging.debug("spell %s has base damage dice %s parsed as:\n%s",
                   spell["name"], m.group(0), pprint(tree))
     return tree
Exemplo n.º 25
0
 def spell(self, spell: Mapping[str, Any], level: int) -> Tree:
     tracer = execution_context.get_opencensus_tracer()
     spell_tree = self.spell_default(spell)
     if level < spell["level_int"]:
         raise ImpossibleSpellError(
             "Sorry, %s is level %d, so I can't cast it at level %d" %
             (spell["name"], spell["level_int"], level))
     m = re.search(r"\d+d\d+( + \d+)?", spell["higher_level"])
     if not m:
         raise ImpossibleSpellError(
             "Sorry, I could't determine the additional damage dice for %s"
             % spell["name"])
     with tracer.span('parse_spell_additional'):
         tracer.add_attribute_to_current_span("name", spell["name"])
         tracer.add_attribute_to_current_span("dice_spec", m.group(0))
         higher_level_tree = get_parser().parse(m.group(0), start="sum")
     logging.debug(
         "spell %s has damage dice %s per extra level parsed as:\n%s",
         spell["name"], m.group(0), pprint(higher_level_tree))
     for level in range(level - spell["level_int"]):
         spell_tree = Tree('add', [spell_tree, higher_level_tree])
     logging.debug("spell %s has complete parsed as:\n%s", spell["name"],
                   pprint(spell_tree))
     return spell_tree
def solution1():
    """
    This solution use trick of permutation.
    """
    query = text("""
    SELECT
        D.name as department,
        E1.name as employee,
        E1.salary as salary
    FROM
        department D,
        employee E1,
        employee E2
    WHERE
        D.id = E1.department_id
        AND E1.department_id = E2.department_id
        AND E1.salary <= E2.salary
    GROUP BY 
        D.id, 
        E1.name
        HAVING COUNT(DISTINCT E2.salary) <= 3
    ORDER BY D.name, E1.salary DESC
    """)
    pprint(query, engine)
def example():
    """
    join Team.id column twice.
    """
    home = aliased(Team)
    away = aliased(Team)

    query = ses.query(Team)
    pprint(query, engine)
    query = ses.query(Match)
    pprint(query, engine)

    query = ses.query(
                Match.id,
                Match.home_team_id,
                home.name.label("home_team"),
                Match.away_team_id,
                away.name.label("away_team"),
            ) \
        .join(home, Match.home_team_id == home.id) \
        .join(away, Match.away_team_id == away.id)
    pprint(query, engine)

    print(query)
Exemplo n.º 28
0
    def execute(self, action):
        char = self.get(action.x, action.y)
        self.put(action.x2, action.y2, char)
        self.put(action.x, action.y, Cell.EMPTY)
        return self

    def pprint_string(self):
        top_bottom = util.color_string('+' + ('-' * self.size) + '+',
                                       fore='blue',
                                       back='blue')
        side = util.color_string('|', fore='blue', back='blue')
        s = top_bottom + '\n'
        for y in range(self.size):
            s += side + ''.join([Cell.color(c)
                                 for c in self.row(y)]) + side + '\n'
        return s + top_bottom


if __name__ == '__main__':
    cmd = util.get_arg(1)
    if cmd:
        state = State(util.get_arg(2))
        if cmd == 'print':
            util.pprint(state)
        elif cmd == 'goal':
            print(state.is_goal())
        elif cmd == 'actions':
            for action in state.actions():
                print(action)
Exemplo n.º 29
0
 def dfs(self, node):
     util.pprint(node.getState())
     self._search("dfs", node)
Exemplo n.º 30
0
 def a_star(self, node, heuristic):
     util.pprint(node.getState())
     self._search("a*", node, heuristic)
Exemplo n.º 31
0
#!/usr/local/bin/python3.9
# coding: UTF-8
import sys
# ty
from util import pprint, execute
from const import PARSERS, REQUESTERS
url = sys.argv[1]
res = {}
for key, binary in PARSERS.items():
    lang, libname = key.split('.', 1)
    r = execute(lang, binary, url, base='bin/parser/')
    res[key] = r
pprint(res)

res = {}
for key, binary in REQUESTERS.items():
    lang, libname = key.split('.', 1)
    r = execute(lang, binary, url, base='bin/requester/')
    res[key] = r
pprint(res)
Exemplo n.º 32
0
 def quit(self, graceful=True, msg='Goodbye'):
     if graceful:
         self.send('QUIT :%s' % msg)
         pprint('Quitting (%s)' % msg)
     exit(0)
employee_list = [
    Employee(
        id=id,
        name=rnd.name(),
        salary=random.randint(50000, 80000),
        department_id=random.randint(1, n_department),
    )
    for id in range(1, 1 + n_employee)
]

ses.add_all(department_list)
ses.add_all(employee_list)
ses.commit()

query = ses.query(Employee)
pprint(query, engine)

query = ses.query(Department)
pprint(query, engine)


def permutation1():
    query = text("""
    SELECT 
        E.id as e_id, 
        E.name as e_name, 
        D.id as d_id, 
        D.name as d_name
    FROM 
        employee as E, 
        department as D
Exemplo n.º 34
0
def print_env():
    pprint(environment())
Exemplo n.º 35
0
 def assertTreeEqual(self, a, b):
     self.assertEqual(
         a, b, "Trees are not equal\n"
         "Tree a:\n%s\nTree b:\n%s" % (pprint(a), pprint(b)))
Exemplo n.º 36
0
                url = url + suffix

        if args.params:
            params = {}
            for s in args.params:
                i = s.index('=')
                name = s[:i]
                value = s[i + 1:]
                params[name] = value

        try:
            init(app_only)
            req = build_request(url, method, params, app_only)
            response = urllib2.urlopen(req)
            if is_stream:
                len_str = ''
                while True:
                    c = response.read(1)
                    if c in string.digits:
                        len_str += c
                    if len_str and c == '\n':
                        length = int(len_str)
                        len_str = ''
                        util.pprint(response.read(length))
            else:
                util.pprint(response.read())
        except urllib2.HTTPError, e:
            print >> sys.stderr, '%s %s' % (e.code, e.msg)
            print >> sys.stderr, e.fp.read()
            traceback.print_exc()
p = json.loads(data, object_hook=lambda d: SimpleNamespace(**d))
print(f"{p.name}, {p.aperture}")

# -------------------------------------

print("# ------------------------------------")
google_cache = {
    'album_ids': {
        "albumid01": {
            'title': "album id title 01",
            'image_count': 21
        },
        "albumid02": {
            'title': "album id title 02",
            'image_count': 22
        }
    },
    'summary': {
        'album_count': 50,
        'image_count': 49330
    }
}
data = json.dumps(google_cache,
                  sort_keys=True,
                  indent=4,
                  default=lambda o: o.__dict__)
google_cache_avatar = json.loads(data,
                                 object_hook=lambda d: SimpleNamespace(**d))
# util.pprint(google_cache_avatar.album_ids.albumid01)
util.pprint(google_cache_avatar)
Exemplo n.º 38
0
def process_instance(context, xfinst, xform_path):
    inst = util.strip_namespaces(et.fromstring(xfinst))
    return convert_odm(context, util.xmlfile(xfinst), load_source(xform_path=xform_path))

def load_source(xform_path=None, crf_path=None):
    if not xform_path and not crf_path:
        return None

    with open(xform_path or crf_path) as f:
        if xform_path:
            return et.parse(f).getroot()
        else:
            return convert_xform(f)

if __name__ == "__main__":
    
    parser = OptionParser()
    parser.add_option("-x", "--xform", dest="xform",
                      help="source xform", metavar="FILE")
    parser.add_option("-f", "--crf", dest="crf",
                      help="source CRF", metavar="FILE")

    (options, args) = parser.parse_args()
   
    inst = (sys.stdin if args[0] == '-' else open(args[0]))
    submission = convert_odm(inst, load_source(options.xform, options.crf))
    print util.dump_xml(submission['odm'], pretty=True)
    del submission['odm']
    util.pprint(submission)

Exemplo n.º 39
0
    def __init__(self, server='localhost', port=6667, nick='ubot', command_prefix='!',
            authorized_users=set(), channels_to_join=set(), rejoin_on_kick=True):
        self.irc_config = {
                'server': server,
                'port': port,
                'nick': nick,
                'user': nick,
                'name': 'ubot v%s' % VERSION,
                'command_prefix': command_prefix,
                'authorized_users': authorized_users,
                'channels_to_join': channels_to_join,
                'rejoin_on_kick': rejoin_on_kick,
        }
        self.init_callbacks()

        pprint('ubot v%s starting up' % VERSION)

        pprint('Authorized users of this bot are (send bot !authlist for on-network list):')
        for i in self.irc_config['authorized_users']:
            pprint(i, indent=4)

        pprint('Legend for colour coded text:')
        pprint('Bot messages (white)', indent=4)
        pprint(colored.cyan('Messages sent to server (cyan)'), indent=4)
        pprint(colored.red('Unimplemented responses (red)'), indent=4)
        pprint(colored.yellow('Private messages (yellow)'), indent=4)
Exemplo n.º 40
0
def ibw2stdout(filename):
    '''Dump the contents of an *.ibw to stdout'''
    data = loadibw(filename)
    util.pprint(data)
Exemplo n.º 41
0
 def send(self, line):
     pprint(colored.cyan('>> %s' % line))
     print(line, end='\r\n', file=self.fp)
Exemplo n.º 42
0
    def play(self):
        while True:

            state = self.current_state
            print(state)
            turn = self.player_turn
            #print(turn)
            self.result = state.game_over()
            result = self.result
            print("Game done?: ", end='')
            print(result)

            if self.result == "X":
                print("Player 1: " + str(sys.argv[1]) + " win the game")
                self.initialize_game()
                return
            elif self.result == "O":
                print("Player 2: " + str(sys.argv[2]) + " win the game")
                self.initialize_game()
                return
            else:
                print("Lets start!")

                if self.player_turn == 'X':
                    #print("Start with X")
                    while self.player_turn == 'X':
                        if sys.argv[1] == 'human':
                            import human
                            run_action = human.HumanPlayer.choose_action(state)
                            print(run_action)
                            for i, action in enumerate(state.actions('X')):
                                #print("looping")
                                if str(i) == run_action:
                                    #print(action)
                                    state.execute(action)
                                    util.pprint(state)
                            self.player_turn = "O"

                        if sys.argv[1] == 'random':
                            import agent
                            run_action = agent.RandomPlayer.choose_action(
                                state)
                            print(run_action)
                            #print(type(run_action))
                            for i, action in enumerate(state.actions('X')):
                                #print("looping")
                                if i == run_action:
                                    #print(action)
                                    state.execute(action)
                                    util.pprint(state)
                            self.player_turn = "O"
                        #print("O turn")
                        #print(self.player_turn)
                    while self.player_turn == 'O':
                        if sys.argv[2] == 'human':
                            import human
                            run_action = human.HumanPlayer.choose_action(state)
                            print(run_action)
                            for i, action in enumerate(state.actions('O')):
                                #print("looping")
                                if str(i) == run_action:
                                    #print(action)
                                    state.execute(action)
                                    util.pprint(state)
                            self.player_turn = "X"

                        if sys.argv[2] == 'random':
                            import agent
                            run_action = agent.RandomPlayer.choose_action(
                                state)
                            print(run_action)
                            #print(type(run_action))
                            for i, action in enumerate(state.actions('O')):
                                #print("looping")
                                if i == run_action:
                                    #print(action)
                                    state.execute(action)
                                    util.pprint(state)
                            self.player_turn = "X"
Exemplo n.º 43
0
				url = url + suffix

		if args.params:
			params = {}
			for s in args.params:
				i = s.index('=')
				name = s[:i]
				value = s[i+1:]
				params[name] = value

		try:
			init(app_only)
			req = build_request(url, method, params, app_only)
			response = urllib2.urlopen(req)
			if is_stream:
				len_str = ''
				while True:
					c = response.read(1)
					if c in string.digits:
						len_str += c
					if len_str and c == '\n':
						length = int(len_str)
						len_str = ''
						util.pprint(response.read(length))
			else:
				util.pprint(response.read())
		except urllib2.HTTPError, e:
			print >>sys.stderr, '%s %s' % (e.code, e.msg)
			print >>sys.stderr, e.fp.read()
			traceback.print_exc()
Exemplo n.º 44
0
async def main():
    signal.signal(signal.SIGINT, bye)
    signal.signal(signal.SIGTERM, bye)

    global conf
    settings = S()

    # init ccxt exchange
    exchange = newExchange(conf)
    conf["market"] = exchange.market

    pprint("tracking OI levels for {}:{}".format(exchange.name,
                                                 exchange.market))

    newOI = asyncio.Event()
    asyncio.create_task(exchange.watchTicker(newOI))
    await newOI.wait()
    newOI.clear()

    t0 = time.time()
    oi0 = exchange.getOI()
    pRef = exchange.getPrice()
    pmin = pRef
    pmin1 = pRef
    pmax = pRef
    pmax1 = pRef

    # will track global delta on custom duration (S.alertInterval)
    oiAlerts = OIDeltas(0, settings, S.alertInterval)
    oiAlertT0 = time.time()
    pAlert = pRef
    oiAlertCircles = ""

    # main data dicts mapping a price range with an OIDelta
    total = {}  # stores OIDeltas for the whole program runtime
    session = {
    }  # partial OIDeltas, works in the same way as total, but is reset every profileTicks
    i = 0

    while True:
        try:
            # fetch ticker data & calculate delta oi (oi - previousOI)
            await newOI.wait()
            newOI.clear()
            oi = exchange.getOI()
            delta = oi - oi0
            oi0 = oi

            # get ticker price, store min/max for session, and calculate price range p
            pReal = exchange.getPrice()
            pmin = min(pmin, pReal)
            pmin1 = min(pmin1, pReal)
            pmax = max(pmax, pReal)
            pmax1 = max(pmax1, pReal)
            p = priceRange(pReal)
            # if p != p0:
            #     print()
            p0 = p

            # check that OIDelta exists for current price range in our data dicts, if not create them
            if not p in total:
                total[p] = OIDeltas(p, settings, settings.d1, settings.d2, 0)
            if not p in session:
                session[p] = OIDeltas(p, settings, settings.d1, settings.d2, 0)

            # retreive OIDeltas object for the current price range, for both dicts
            oidTotal = total[p]
            oidSession = session[p]
            # add current delta
            tasks = [
                asyncio.create_task(oidTotal.add(delta)),
                asyncio.create_task(oidSession.add(delta)),
                asyncio.create_task(oiAlerts.add(delta)),
            ]
            await asyncio.gather(*tasks)
            # print current level
            pprint("{}        OI: {:>16,.0f}".format(oidTotal, oi))

            # increment main tick
            i += 1

            # check if we reached alert threshold
            f = oiAlerts.frames[S.alertInterval]
            if math.fabs(f.value) >= S.alertThreshold:
                # circles mic-mac
                blue_diamond = "🔹"
                orange_diamond = "🔸"
                if f.value > 0:
                    if orange_diamond in oiAlertCircles:
                        oiAlertCircles = ""
                    oiAlertCircles += blue_diamond
                else:
                    if blue_diamond in oiAlertCircles:
                        oiAlertCircles = ""
                    oiAlertCircles += orange_diamond

                alertsDuration = time.time() - oiAlertT0
                alertsDuration = min(alertsDuration, S.alertInterval)
                msg = "*{}:{}* - {:.1f} (*{:+,.1f}*)\noi: {:+,.0f} in {:.0f}s {}\nmin/max: {:.1f}/{:.1f} ({:.1f})".format(
                    conf["exchange"],
                    conf["market"],
                    pReal,
                    pReal - pAlert,
                    f.value,
                    alertsDuration,
                    oiAlertCircles,
                    pmin,
                    pmax,
                    pmax - pmin,
                )
                pprint("alert reached")
                print(msg + "\n")
                if alertsDuration <= S.alertInterval:
                    try:
                        telegram.sendMessage(msg)
                    except Exception as err:
                        pprint(err)
                # reset alert data
                await oiAlerts.cancel()
                oiAlertT0 = time.time()
                f.value = 0
                pAlert, pRef, pmax, pmin = pReal, pReal, pReal, pReal

            # check if current profile session is elapsed or not
            if i % S.profileTicks == 0:
                # current session is elapsed, S.profileTicks reached, so we print profile summary
                print()
                pprint("profile for %s:%s, last %.0f minutes:" % (
                    exchange.name,
                    exchange.market,
                    (time.time() - t0) / 60,
                ))
                t0 = time.time()
                totalDelta = 0
                for p, oid in sorted(session.items(), reverse=True):
                    print("  {}".format(
                        oid.repr(last=False,
                                 ignore=(settings.d1, settings.d2))))
                    totalDelta += oid.frames[0].value
                print(
                    "\n    ticker: {} ({})  min/max: {:.1f}/{:.1f} (<> {})  oi: {}\n"
                    .format(
                        pReal,
                        coloredValue(pReal - pRef,
                                     1,
                                     threshold=50,
                                     padSize=4,
                                     decimals=1,
                                     plus=True),
                        pmin,
                        pmax,
                        coloredValue(pmax - pmin,
                                     1,
                                     threshold=100,
                                     padSize=4,
                                     decimals=1),
                        coloredValue(totalDelta,
                                     S.interval * S.profileTicks,
                                     threshold=S.threshold),
                    ))
                # reset current profile summary session
                session = {}
                pRef, pmax, pmin = pReal, pReal, pReal
        except:
            logging.exception("unhandled exception")
import context
context.set_context()
import util
import tasks

dups = tasks.find_duplicate_pics_folder_images("p:\\pics")
util.pprint(dups)
Exemplo n.º 46
0
# Files to use in this example
schemaFile = './schemata/mide_ide.xml'
ebmlFile1 = './tests/example-ebml.ide'
prettyXml = './tests/example-pretty.txt'

# load the schema to use with these files.  This creates an object that is used
# to parse EBML files.
schema = core.loadSchema(schemaFile)

# load the ebml file into memory
ebmlDoc1 = schema.load(ebmlFile1,
                       headers=True)  # load the file through the schema

# save the ebml to an easily readable format
pprint(ebmlDoc1, out=open(prettyXml, 'wt'))

# =======================================
# Get specific data from an EBML file
# =======================================

# Files to use in this example
schemaFile = './schemata/mide_ide.xml'
ebmlFile1 = './tests/example-ebml.ide'

# load the schema to use with these files.  This creates an object that is used
# to parse EBML files.
schema = core.loadSchema(schemaFile)

# load the ebml file into memory
ebmlDoc1 = schema.load(ebmlFile1,
Exemplo n.º 47
0
			util.save_to_json()
		elif args.url:
			url = args.url
			method = args.method.upper()
			if not url.startswith('https://'):
				url = prefix + url
			params = _get_params(args.params)
			init()
			params['access_token'] = config.facebook_access_token
			if method == 'GET':
				url = url + '?' + urllib.urlencode(params)
				req = urllib2.Request(url)
			elif method == 'POST' or method == 'PUT':
				req = urllib2.Request(url)
				req.data = urllib.urlencode(params)
				req.add_header('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
				req.get_method = lambda: method
			else:
				url = url + '?' + urllib.urlencode(params)
				req = urllib2.Request(url)
				req.get_method = lambda: method

			response = urllib2.urlopen(req)
			util.pprint(response.read())
		else:
			parser.print_help()
	except urllib2.HTTPError, e:
		print >>sys.stderr, '%s %s' % (e.code, e.msg)
		print >>sys.stderr, e.fp.read()
		traceback.print_exc()
def do_work(et, google_image_filter, album_folder_path, list_only):

    # Find folder album in the database
    LocalLibrary.load_library('raw')
    local_library_cache = LocalLibrary.cache_raw()
    images = local_library_cache['images']
    albums = local_library_cache['albums']
    album_paths = local_library_cache['album_paths']

    album_idx = album_paths[album_folder_path]
    album = albums[album_idx]
    local_album_path = album['path']

    print(f"[INFO]: Found album '{local_album_path}'")

    # Collect list of local album files
    local_files_results = []
    local_album_images = album['images']
    for image_idx in local_album_images:
        image = images[image_idx]
        image_name = image['name']
        image_path = image['path']
        local_files_results.append(image_path)

    sorted(local_files_results)
    util.pprint(local_files_results)
    print(f"[INFO] Local files count '{len(local_files_results)}'")

    # Collect a list of images from google photos
    # Each element in this list will be an object:
    #     {'path': 'image path', 'caption': 'images caption...'}
    google_images_results = []
    gphoto.init()
    GoogleImages.load_images()
    google_image_cache = GoogleImages.cache()
    google_images = google_image_cache['list']
    for google_image in google_images:
        image_name = google_image['filename']
        if image_name.find(google_image_filter) < 0:
            continue
        image_desc = google_image['description']

        google_images_results.append((image_name, image_desc))

    google_images_results = sorted(google_images_results,
                                   key=lambda record: record[0])
    util.pprint(google_images_results)
    print(f"[INFO] Google files count '{len(google_images_results)}'")

    # Perform basic validations
    # If counts are not the same then error out
    if len(local_files_results) != len(google_images_results):
        print(
            f"[ERROR]: Count mismatch local: '{len(local_files_results)}', google: '{len(google_images_results)}'.  Aborting"
        )

    # Now loop through the list of folder images, get its
    # equivalent caption from the corresponding google image
    if list_only:
        return

    for image_idx, local_image_path in enumerate(local_files_results):
        desc = google_images_results[image_idx][1]

        # Get image extension and identify it as an image or video
        image_name = os.path.basename(local_image_path)
        image_ext = ImageUtils.get_file_extension(image_name)
        is_video = ImageUtils.is_ext_video(image_ext)

        # Set the caption now
        ImageUtils.set_caption(et, local_image_path, desc, is_video)
Exemplo n.º 49
0
def main():
    """
    Given an image pattern find the image and does it have a
    parent album.  Also find other related images
    Arguments:
        <patterns>: List of Image pattern
    """
    if len(sys.argv) < 2:
        logging.error("Too few arguments.  See help")
        return

    # Get arguments
    patterns = sys.argv[1:]

    # Load cache
    GoogleLibrary.load_library()
    cache = GoogleLibrary.cache()
    google_album_ids = cache['album_ids']
    google_album_titles = cache['album_titles']
    google_image_ids = cache['image_ids']
    google_image_filenames = cache['image_filenames']
    google_album_images = cache['album_images']
    google_image_albums = cache['image_albums']

    # Define the result
    #   result = {
    #       'seed_image': {         #  first Images that matched the pattern
    #           'id': ...,
    #           'filename': ...,
    #           'creationTime'
    #       },
    #       'albums': [
    #           "album id": {
    #               'title': ....,
    #               'images': [
    #                   {
    #                       'id': ...,
    #                       'filename': ...,
    #                       'creationTime'
    #                   },
    #                       .....more imahes...
    #               ]
    #           },
    #               .... more albums ...
    #       ]
    #   }

    result_albums = []
    result = {'albums': result_albums}

    # Find images with the give patterns
    for google_image_id, google_image in google_image_ids.items():
        google_image_filename = google_image['filename']
        result_pattern = [p for p in patterns if p in google_image_filename]
        if result_pattern:

            # Add image to the result
            result['seed_image'] = {
                'id':
                google_image_id,
                'filename':
                google_image.get('filename'),
                'creationTime':
                google_image.get('mediaMetadata').get('creationTime')
            }

            # Get list of parent albums
            result_image_albums = google_image_albums.get(google_image_id)
            if result_image_albums is not None and len(
                    result_image_albums) > 0:

                # For each album id in image parent list add it to the result
                for result_image_album_id in result_image_albums:
                    google_album = google_album_ids.get(result_image_album_id)

                    image_list = []
                    result_album = {
                        'id': result_image_album_id,
                        'title': google_album.get('title'),
                        'images': image_list
                    }

                    result_albums.append(result_album)

                    # Get the list of all images for this album and add to the result
                    result_album_image_ids = google_album_images.get(
                        result_image_album_id)
                    for result_album_image_id in result_album_image_ids:
                        image = google_image_ids.get(result_album_image_id)
                        image_list.append(image.get('filename'))
                    image_list.sort()

            break

    util.pprint(result)
def problem1():
    """
    find all numbers that appear at least three times consecutively.
    """
    query = ses.query(Number.value).order_by(Number.id)
    number_list = [number.value for number in query]

    init = 0
    count = 0
    n_consecutive = 3
    target_numbers = list()
    for i in number_list:
        if i != init:
            init = i
            count = 1
        else:
            count += 1
        if count == n_consecutive:
            target_numbers.append(init)

    # print(number_list)
    # print(target_numbers)

    # pprint(query, engine)

    query = ses.query(Number.value, func.lag(Number.value, 1))
    pprint(query, engine)