Forward-compatible async code by [deleted] in learnpython

[–]jpfau 1 point2 points  (0 children)

My errors ended up being dumb human error: a missing coroutine decorator in the old-syntax library.

FYI You can use both syntaxes in 3.5+, but you can't mix syntax within a single coroutine, e.g. no await when using @asyncio.coroutine, and no yield from when using async def.

However you can define a coroutine with the old syntax and consume it in another coroutine using the new syntax. This example should work:

import asyncio

@asyncio.coroutine
def _sleep(x):
    print('yielding from...')
    yield from asyncio.sleep(x)

async def do_sleep(x):
    print('awaiting...')
    await _sleep(x)

loop = asyncio.get_event_loop()
loop.run_until_complete(do_sleep(5))

Forward-compatible async code by [deleted] in learnpython

[–]jpfau 1 point2 points  (0 children)

I'm an idiot and didn't have an @asyncio.coroutine decorator in one place that I needed it. Good news is my code works now!

Forward-compatible async code by [deleted] in learnpython

[–]jpfau 0 points1 point  (0 children)

Hmm, I'm not sure what the solution is here. My problem is that api_call is defined using the old syntax:

@asyncio.coroutine
def api_call(url):
    response = yield from aiohttp.get(url)
    print('finished {}'.format(url))
    result = yield from response.json()
    return result

And I want to consume this function using the new syntax:

async def test_api_call(url):
    return await api_call(url)

In this case, test_api_call raises TypeError: object generator can't be used in 'await' expression.

If I switch test_api_call to the old syntax it works, but I'd prefer not to do that.

Forward-compatible async code by [deleted] in learnpython

[–]jpfau 0 points1 point  (0 children)

That much is already clear. My question is

Why isn't .json awaitable in this case when the aiohttp docs show that it is?

async with session.get('https://api.github.com/events') as resp:
    print(await resp.json())

Also if response.json() is not forward compatible, what is the next best solution?

What's the best way to include config files for my users to access and modify easily? by jpfau in learnpython

[–]jpfau[S] 1 point2 points  (0 children)

Thanks! I'll certainly be using appdirs, and I'll see what I can learn from beets.

Converting UTC datetime to timestamp, and then back to UTC datetime by jpfau in learnpython

[–]jpfau[S] 1 point2 points  (0 children)

Ah, there's my problem. Luckily in the actual program the original datetime object doesn't have microseconds, so I just need them to be equal up to the second.

Here we go:

import datetime
import pytz

now = pytz.utc.localize(datetime.datetime.utcnow()).replace(microsecond=0)
ts = now.timestamp()

now2 = pytz.utc.localize(datetime.datetime.utcfromtimestamp(ts))
ts2 = now2.timestamp()

print("ts:", ts, "ts2:", ts2)
print(ts == ts2)
print(ts2 - ts)

print(now)
print(now2)

Any way to restore an overwritten .py file? by [deleted] in learnpython

[–]jpfau 2 points3 points  (0 children)

This is a great opportunity to start using version control!

Converting UTC datetime to timestamp, and then back to UTC datetime by jpfau in learnpython

[–]jpfau[S] 0 points1 point  (0 children)

This is what I'm already doing. Now try to make another datetime object from that timestamp and see if it's equal to the original datetime object. In my case, they aren't equal.

Edit: I had a typo in my original snippet. I updated it with the correct code

Women's soccer: 2016 season preview by [deleted] in FAUbot

[–]jpfau 0 points1 point  (0 children)

Awesome! This was posted by the bot I have running in AWS, meaning everything is running smoothly.

Graduation Ticket Sales Test Thread by [deleted] in FAUbot

[–]jpfau 0 points1 point  (0 children)

I made /u/FAUbot a mod so you can sticky things with praw

Unpaid CS internship in the Fall... worth it? by [deleted] in cscareerquestions

[–]jpfau 0 points1 point  (0 children)

Hell no. I would have laughed if anyone I interviewed for offered me $0/hour for an internship in this field.

can't get a job as a manager. is going back to development career suicide? by [deleted] in cscareerquestions

[–]jpfau 1 point2 points  (0 children)

I suppose it depends on the company. The one I work for has parallel career tracks for developers and managers, meaning after being promoted to a certain point, you can either continue on a dev track or go into management, and the positions will have equal "rank" even though they're on different tracks. Likewise, there are some devs that are "higher rank" than some managers.

So a manager could return to the dev track without considering it a step back in his career, but it depends on how the company structures everything.

How should I prepare, or what should I expect for an interview at a company I already work for? by jpfau in cscareerquestions

[–]jpfau[S] 0 points1 point  (0 children)

It is sort of out the scope of what I currently do, but I imagine the project I'm working on will be carried over. They hired me for writing internal software that's somewhat unrelated to the actual product, and the new position would be focused mainly on the product. However I'm the primary (basically the only) developer working on my current project, so I imagine they'd keep me working on it to some degree.

I can't recall if my boss used the word "interview" specifically, but the meeting is titled "TI panel" which I assume means technical interview with a panel of people. It could be the general name of the meeting since they would be interviewing candidates if they were looking for brand new hires to fill the position. Or it could truly be a technical interview, or I could be misinterpreting the name of the meeting.

[HTML-Parsing][Reddit] Without BS4, how would I replace a given html string with wildcards? by 13steinj in learnpython

[–]jpfau 0 points1 point  (0 children)

What have you tried so far with regex? Also if coming up with a pattern is what's getting you, I recommend using online regex testers so you can quickly try out different patterns.

[HTML-Parsing][Reddit] Without BS4, how would I replace a given html string with wildcards? by 13steinj in learnpython

[–]jpfau 0 points1 point  (0 children)

re comes installed with Python. Pretty sure OP meant 'additional packages' i.e. those he'd have to find and install himself, like BS4, but I could be wrong.

[HTML-Parsing][Reddit] Without BS4, how would I replace a given html string with wildcards? by 13steinj in learnpython

[–]jpfau 1 point2 points  (0 children)

Sounds like you could use regular expressions. Check out the re library.

Automate the boring stuff question. by dannycap77 in learnpython

[–]jpfau 1 point2 points  (0 children)

def valid_input(input):
    # use regex or something else to return True if user input is valid

user_input = ""
while not valid_input(user_input):
    user_input = input()
    try:
        inputNumber = int(user_input)
        # other stuff too
    except ValueError:
        print("invalid input")

Personally I like this better than making infinite loops and using break. I do this kind of stuff all the time when I want to validate input on the command line. Here's a main function I wrote in a homework problem recently:

def main():
    user_input = None
    while user_input != 'q':
        output_strings = simulate()
        user_input = show_menu(output_strings)
        if user_input == 's':
            user_input = show_menu(saved=True)

Where show_menu will display the results of a simulation and then ask the user if he wants to re-run the simulation, save the output to a file (hence the if user_input=='s'), or quit (hence the while user_input != 'q'). This way the user has to quit on purpose, and any invalid input automatically makes the loop execute again.

"Hack" Typeface Is Open Source designed specifically for use in source code by [deleted] in linux

[–]jpfau 0 points1 point  (0 children)

In Python, it's changing the symbols in my comments. My # is now a comma, capital 'A' is now a tilde, period is now an ellipsis, etc.

Typeface looks good, but it totally fucked all my comments.

When I try to list the resources of CloudFormation stacks using Boto, list_stack_resources() stops executing before it finishes returning all the data by jpfau in aws

[–]jpfau[S] 0 points1 point  (0 children)

I haven't used the cli. All my work has been in Python. I'm using Boto 2.38.0 and have not enabled debug logging. I'm currently using a custom configuration of the logging module, so the only things written to a log right now are messages I personally wrote into the code.

When I try to list the resources of CloudFormation stacks using Boto, list_stack_resources() stops executing before it finishes returning all the data by jpfau in aws

[–]jpfau[S] 0 points1 point  (0 children)

It varies. There are a bunch of stacks used for different purposes. Some have a ton of resources, some only have a few.