Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

LochNessMonster posted:

I just ditched PyCharm because I couldn't really get used to it and installed anaconda on my work laptop (windows 10).

This is a weird thing to say, so just to make sure you didn't accidentally type the wrong thing...you know that PyCharm is an IDE and Anaconda is a package manager?

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

huhu posted:

I could see Pycharm being overwhelming if you've never programmed before. Perhaps better to start off with something more familiar feeling first like Sublime Text. That being said, I moved from Sublime to Pycharm in my learning progression and never plan to go back.

Right. But using or not using Anaconda doesn't have anything to do with PyCharm.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

LochNessMonster posted:

RE: Miniconda instead of Anaconda.

Same poo poo, also doesn't work. So I guess it's a computer specific issue. Not going to put more time into this. I'll just run all my stuff from bash and manage packages with pip.


Hey, I don't know if it works but you might want to try using the Linux Miniconda inside bash.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

LochNessMonster posted:

Won't that mess things up?

No. If you use pip (without using virtualenv) it install packages into your system python install.

When you use miniconda it has its own python install that won't have anything to do with your system python.

FWIW, I always use pip when I'm using miniconda. Mainly because it works just as well as `conda install` does in 99% of cases, and pip is what everyone uses.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Seventh Arrow posted:

2) I'm still kind of new to programming and there's a lot of stuff to take in/remember. I assume that people usually don't just code things from scratch - do you keep cheat sheets around or what?

Kind of. I call it Google.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell


oh gently caress yes

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

KernelSlanders posted:

Not everyone in this thread builds web apps.


This thread has always been open to beginners. The poster who has been asking questions is obviously a beginner. Floating point stuff obviously is weird to beginners.

Thermopyle fucked around with this message at 17:44 on Jun 11, 2017

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Nippashish posted:

Python code:
>>> abs(.3 / 3 - 0.1) < 1e-12
True
They're weird when they're small. One of the implications is you shouldn't expect exact equality.

That's why its confusing to beginners.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

huhu posted:

What would be the best way to take:

[{u'amt': 10.0}, {u'amt': 0.1}, {u'amt': 1.0}, {u'amt': 3.0}, {u'amt': 5.0}]

and turn it into

[10.0, 0.1, 1.0, 3.0, 5.0]

Python code:
x = [{u'amt': 10.0}, {u'amt': 0.1}, {u'amt': 1.0}, {u'amt': 3.0}, {u'amt': 5.0}]
[d['amt'] for d in x]

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Async basically only helps with IO-bound tasks. If you are mostly CPU-bound you should probably use multiprocessing.

(I'm on phone and basically didn't even look at your code)

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Malcolm XML posted:

Lol use an actual diffeq solving codes instead of doing it by hand unless you are researching solvers


Malcolm XML posted:

Like instead of the janky first order stuff you could probably use the standard rk4 and be algorithmically faster

Did this thread get moved to yospos?

(I don't disagree)

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

huhu posted:

Does anyone have a suggestion for a tutorial that walks through the basics of sockets up to putting it on a server and being able to type in "huhu.com/index.html" and get a page to load? I've got something basic running locally but would like to explore sockets, wsgi, and related topics more in depth.

Google "python web server from scratch". There's a bunch of decent-looking stuff on the first page.

Also you can look over the code to Python's own simple http server. https://github.com/python/cpython/blob/3.6/Lib/http/server.py

Thermopyle fucked around with this message at 20:21 on Jun 22, 2017

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

People complain about the same thing in javascript all the time. It's just the standards-defined behavior we're stuck with.

As for the why, I think it boils down to the limitation of the hardware and software available at the time the standard was created. See this SO answer and the related comments.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Mirthless posted:

I'm using classes already though maybe incorrectly?

I'm using functions for things like class/race selection, then returning the value to the dictionary when i call the function, like if you pick a Half-Elf Fighter or whatever the Fighter class gets assigned to Player['Class'] and the Half-Elf class gets assigned to Player['Race'] etc


code:
class Fighter(CClass):          
    HitDie = 10
    FighterCon = True
    HumanAge = 15 + (randint(1, 4))
    DwarfAge = 40 + (randint(5, 20))
    ElfAge = 130 + (randint(5, 30))
    GnomeAge = 60 + (randint(5, 20))
    HalfElfAge = 22 + (randint(3, 12))
    HalflingAge = 20 + (randint(3, 12))
    HalfOrcAge = 13 + (randint(1, 4))
    StatRequirements = 9,7,0,0,0,0

In case you don't realize this, this is going to give all your Fighter instances the same stats, but different stats each time you run your program.

The randint is just going to be added to those class attributes when the file is parsed on load, not each time the class is instantiated.

Python code:
from random import randint


class Fighter:
    HumanAge = 15 + (randint(1, 40))


class BetterFighter:
    def __init__(self):
        self.human_age = 15 + (randint(1, 40))


if __name__ == "__main__":
    fighter_ages = [Fighter().HumanAge for _ in range(100)]
    better_fighter_ages = [BetterFighter().human_age for _ in range(100)]

    print('Unique fighter ages:', len(set(fighter_ages)))
    print('Unique better fighter ages:', len(set(better_fighter_ages)))

code:
>python blah.py
Unique fighter ages: 1
Unique better fighter ages: 36

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Boris Galerkin posted:

In PyCharm how do I tell it to do the "optimize imports" thing but also to ignore specific lines? Basically I need to keep

code:
import os, sys
sys.path.insert(0, some_path)
as the top first two lines in some of my scripts before any other imports happen.


I seemed to recall seeing an issue on their issue tracker, and a quick search revealed my memory was correct! So, to answer your question: you vote for this issue.

As an aside, generally I've found JetBrains to be fairly responsive to their issue tracker if you write good issues.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Loezi posted:

What's the easiest library to make a GUI with, given that I require hassle-free licensing (which rules out PyQt)?

Native elements are appreciated but not necessarily required.

Keep in mind that most answers to these type of questions will come from people who have only seriously used one or two of the options.

I've used 4 different options on pretty large applications and I'd say you'll have the easiest time with HTML/CSS/JS + some python web framework and a browser.

However, every solution has its pros and cons and its hard to say which is actually easiest when there's so many variables from your experience level to the type of application you're developing to your ability to handle context switching between paradigms.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Electron will work with python 3. Electron doesn't care what language you use on the "backend".

Also there is a browser starter-upper thing in the standard library, IIRC. I just can't remember what it's called and I'm on my phone right now and don't feel like going through the effort.

Thermopyle fucked around with this message at 16:41 on Jul 31, 2017

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Depending upon what your crawler is actually doing, you should probably use asyncio or greenlet instead of threads/multiprocessing anyway. I/O bound tasks work great with async.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Nippashish posted:

Is it possible to use async without making the code async "all the way down"? Maybe I'm just dense, but afaict anything that blocks in a non-async way (read: all legacy code ever) ends up blocking the world?

In general, there's two ways that async code gets blocked. 1. You use non-async-aware IO code or 2. you become CPU bound. In either case you're all blocked up.

If your code is substantially CPU bound, async is the wrong tool. On the other hand, if you're dealing with non-async-aware code you have several options.

The absolute easiest is to use eventlet or greenlet which will patch the standard library at runtime to make all the I/O functions (like the socket library and thus http, urllib, requests, etc) use yielding versions. This will make your legacy python code automatically async-friendly.

Another option is that if the right subset of your code is CPU bound or is not monkey-patch-able to make it async friendly is to spin up another process or thread to run that code in.

Either way, if your code is substantially bound up with IO operations async can make it faster than threads or processes.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

You have to be careful though. It seems like magic that you can make your code async-ready automatically, but its easy to have CPU-bound code hidden amongst your legacy code and then after using the monkeypatching abilities of greenlet you think "this poo poo don't work gently caress you".

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Newf posted:

Do you know (or care to share) any os projects with a python / electron setup? I've only glanced at electron, and then only with JS/TS projects. Haven't seen used as a UI layer with other languages.

Theres a few around. I've done a fairly large project used internally by a company in their line of business so I can't share that. The one that comes to mind is called Rodeo. Google around for that.

I haven't used it, but an alternative to Electron is something called cefpython...someone might want to look in to that.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

fletcher posted:

How do I avoid having import statements below my logging config? My IDE tells me all the import statements should be at the top, but I need to set the logging config before some of these libraries are loaded. This is for a little CLI python app.

code:
import logging
import logging.config

logging.config.dictConfig({
    'blah': 'blah'
})

import whateverlib

# rest of my app

Your IDE doesn't always know where import statements should go. Style guides are just guides.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Say i have a routine that searches for and gets a thing. You can search for a thing via a regex, via an int, or via a tuple. Which of the following do you prefer?

one
Python code:
 
def find_thing(filter):
  # code that type checks 'filter' and does the right thing.
two
Python code:
 
def find_thing_regex(regex):
def find_thing_int(an_int):
def find_thing_tuple(a_tuple):
three
Python code:
def find_thing(regex=None, an_int=None, a_tuple=None):

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I don't know why I didn't even think of using a predicate since most of my time is spent doing JS nowadays and thats a very common pattern there. Thanks for reminding me.

Now I accept a predicate, or build a predicate for you if you provide the other types I mentioned above.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Well gently caress, how did I not remember single dispatch?

I'm happy enough with what I've got going now, but I certainly agree with PEP-443 where they introduce single dispatch where it says:

quote:

In addition, it is currently a common anti-pattern for Python code to inspect the types of received arguments, in order to decide what to do with the objects.

For example, code may wish to accept either an object of some type, or a sequence of objects of that type. Currently, the "obvious way" to do this is by type inspection, but this is brittle and closed to extension.

Thats exactly the reason I always get the heebie-jeebies when I see a function body starting out with a bunch of isinstance checks.

Thermopyle fucked around with this message at 19:02 on Aug 6, 2017

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

outlier posted:

A Flask question, maybe just a style one.

I'm tidying up a Flask web-service and was just going to stick in the footer:

- The current number of records ("Currently storing 323,000 foos!")
- A link to the latest news item (which are also stored in the flask db)

But it's not kosher to stick this sort of logic in footer template. And it's another two queries for each page. And there's no need to call (and recall) these two queries for every page, as they don't have to be precisely up to date.

So what's the neatest way to do this?

Cache the database results somewhere (memcache). Update the cached values when new news item posted and every X period of time depending on how accurate you want the number of records to be.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Probably, most people who need to read csv's don't need pandas, but it certainly seems like this person could use it.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I've not wrapped my head around how this UE4 python thing works (in fact, I don't anything at all about UE4), but what about using modules as singletons?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

(You should use requests, not urllib3...but probably not worth switching since you've already done this.)

1. Parsing HTML tables is usually weird and gross, so I also usually don't really like the code that does it either. I'm too lazy to actually look at the page and figure out if you're doing it the best.

2. What do you want on the web exactly? Each time you visit a url it should run your code to fetch the data? Do you want to run it once a day and then store the results for display any time during the day? "Web service" implies you want to serve it in a machine readable way like JSON...is that what you want or are you wanting to display a HTML site with your data?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

same

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

chutwig posted:

If Python had enforcement on type hints and a static binary generator that wrapped up cx_freeze/py2exe/py2app into pythonc, would Go have ever left the launchpad?

This isn't what you're talking about, but you reminded me that I read about it earlier today.

quote:

Nuitka is a Python compiler.
...
You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
...
Right now Nuitka is a good replacement for the Python interpreter and compiles every construct that all relevant CPython version, and even irrelevant ones, like 2.6 and 3.2 offer. It translates the Python into a C program that then is linked against libpython to execute in the same way as CPython does, in a very compatible way.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Linear Zoetrope posted:

Do you need to touch the reference counters or GIL if you want to spawn a bunch of threads from the C side, call Python functions that return PyObjects, and forward the PyObjects between the threads? To be clear, only one thread will touch a given PyObject * at a time.

It's been awhile since I did this, but I'm fairly sure the answer is "no".

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

StackOverflow thinks python is the fastest-growing language. Started to outpace all other languages in 2012 in high-income countries and 2014 in lower-income countries.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

NtotheTC posted:

So my new project is sufficiently out of the quagmires of legacy code that I get to start using the latest versions of everything- including python3.6 over python2.7. It's perhaps a bit shameful that I've done virtually no commercial coding with python3, but I'm wondering if there's a list of the biggest changes I'll need to wrap my head around? f_string_literals and type hinting are two that I've been made aware of but I'm sure there's more out there.

I just came across this article today:

http://www.b-list.org/weblog/2017/sep/05/how-python-does-unicode/

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

As a followup to that thing I posted from stackoverflow about how Python is the fastest growing language, they delve into why its the fastest growing language.




its data science

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Boris Galerkin posted:

Is there a conda equivalent to "pip download -r requirements.txt"?

I have an environment set up and I'd like to export it along with all the required downloaded things to install on non networked computers.

I found this link

https://stackoverflow.com/questions/32956583/how-do-i-download-anaconda-packages-without-installing-them

Which links to the official FAQ but that question/answer has been removed.

Just use pip inside your conda environment.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

You'll find that pure functions lead to better programs.

Generally, do
Python code:
return reversed(my_list)
instead of
Python code:
my_list.reverse()
return my_list

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I disagree that that is the best way if only because it's a little esoteric, whereas reversed is very explicit. I mean, I'm not going to puke when I see the slicing syntax, but it's going to be my third choice.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Slimchandi posted:

I'm learning more about how to structure a program having written mostly scripts up until now. Please can someone check my understanding and point me towards useful resources if I'm fundamentally wrong.

I understand that if I write a separate.py file, and import it in my main program by using 'import separate' then (almost) all the code in that .py file is executed, and the functions are available in the main program as separate.func1(), separate.func2() etc. Objects that are created when separate.py runs (such as pulling in some data from a file) are accessible to separate's functions, but not in the main file. Any code under 'if __main__' is not executed, as separate was imported, not ran.

What happens if I do 'from separate import func1’? How much of the code in separate.py is executed? I want to do some fairly expensive curve fitting and dataframe building, but if I have to put this code in my func1 definition this seems like a complete waste fitting the same data every time the function runs.

Any general 'beyond python scripting' resources gladly received.

Anything that is accessible in your "if main" code is also accessible to whatever imports your "separate" module.

If you import func1 from separate then your func1 still has access to everything in your separate module, but code in your module that imported separate does not have access to anything but func1.

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

The easiest way to modify it to do what you want is to add this between line 30 and 31:

Python code:
if 'data' not in category.lower(): 
    continue

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply