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
Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
I don't use pylint, only the linter that's built in to PyCharm -- does pylint flag implicit string concatenation? At first glance it seems tricky to figure out when it might be intentional, since I relatively commonly do

Python code:
p = ArgumentParser()
p.add_argument(
    '--some-option',
     help=(
        'a long description that '
        'I split across multiple lines'
    ),
    action='store_true',
)
but I virtually never want implicit string concatenation in a List[str] like

Python code:
some_strings = [
    "One",
    "Two",
    "Three",
]
so it seems like I'd get a lot of use out of a simple heuristic like "found implicit string literal concatenation inside a list literal without enclosing parentheses".

Adbot
ADBOT LOVES YOU

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

HardDiskD posted:

It's throwing a syntax error because it's missing the except clause.

By the way, you can rewrite any file opening operation as:

Python code:
with open('filename') as file:
    # ... Do things with the file
#... Program goes on
Doing this way makes Python close the file automagically.

except clauses are not required if there's a finally.

code:
In [1]: try:
   ...:     print(undefined_name)
   ...: finally:
   ...:     print('finally')
   ...:     
finally
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-3f894de109e3> in <module>()
      1 try:
----> 2     print(undefined_name)
      3 finally:
      4     print('finally')
      5 

NameError: name 'undefined_name' is not defined

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
This message is almost certainly not from the Python interpreter itself:

rt4 posted:

code:
There was an error:

It's more likely that somewhere in the code, there's a try/except block that's catching an exception and only printing out the exception's message, like:
Python code:
try:
    # some stuff here
except SomeExceptionType as e:
    # afaik e.message is deprecated or removed
    print(e.args[0])
You could add a bare raise inside whatever appropriate except block to have the exception propagate upward, which will cause the Python interpreter to show you the full traceback (with line numbers, function names, and file names).

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

huhu posted:

code:
rootPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'temp')
targets = [target for target in os.listdir(rootPath) if os.path.isdir(os.path.join(rootPath, target))]
for target in targets:
    xPath = os.path.join(rootPath, target, "x")
    xFile = os.listdir(xPath)[0]
    with open(os.path.join(xPath, xFile) "r") as f:
        for line in f:
            print(line) # Whatever commands I'd actually need to do with this line here. 
Is there a cleaner way to not have so many os.path.join() or would that require knowing the operating system I'm working with?

1. Use pathlib:
Python code:
from pathlib import Path

rootPath = Path(__file__).parent / 'temp'
subdirs = [child for child in rootPath.iterdir() if child.is_dir()]
for subdir in subdirs:
    xPath = subdir / "x"
    xFile = next(iter(xPath.iterdir()))
    with open(xPath, "r") as f:
        for line in f:
            print(line) # Whatever commands I'd actually need to do with this line here. 
2. Do you really just want to process the first file in each 'x' subdirectory? Do you know that it isn't guaranteed to be the first file you see when filenames are sorted alphabetically?
3. To find any file in any subdir, use os.walk and use fnmatch to check whether you're interested in it based on the filename.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Dominoes posted:

If you haven't tried Pipenv, give it a shot: It elegantly combines virtualenvs with pip, and has simplified my workflow. It's been hard-broken with two distinct bugs until a few days ago, but the latest version is good-to-go.

Does pipenv still insist on using the deprecated virtualenv tool on all versions of Python, instead of using the venv standard library module when it's available? That was definitely the case when I checked last, and was a no-go for me to try out pipenv.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Sockser posted:

When building out some code last week, I accidentally used {} to make an array instead of ()
e.g. arr = { ‘a’, ‘b’, ‘c’, }

This was working but the order was getting goofed, which led me to discover my error. Is this just generating a dictionary of only keys with None values?

No, this creates a set object: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset

Sets are unordered collections of distinct objects, so the order you see is arbitrary, and duplicate elements are only stored once:

code:
In [1]: {'a', 'b', 'c', 'b'} == {'c', 'a', 'b'}
Out[1]: True
I'm pretty sure that in the distant past (like Python 2.3), sets were implemented as dictionaries that mapped every key to None, but this is an implementation detail that isn't necessarily still true. I'm sure the set and dict implementations in current Python share a lot of hash table functionality, but I wouldn't be surprised if sets were no longer implemented as wrapper classes around dicts that map each element to None.

e: wow beaten twice, that was fast

Lysidas fucked around with this message at 20:14 on Dec 20, 2017

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Are you absolutely certain that you are communicating with a different server when you access (e.g.) https://www.company.com/some-path vs. https://www.company.com/? That is generally not how DNS, TCP/IP, TLS/SSL, and HTTP work -- one cannot have a specific URL path served by a different publicly accessible web server, with a different certificate, from the same hostname.

When you connect to https://www.company.com/some-path, the following happens, more or less:
  1. Your system performs a DNS query for www.company.com
  2. Your TLS/SSL library connects to the appropriate IP address over port 443
  3. Your HTTP client uses the TLS-wrapped socket to request the path /some-path with hostname www.company.com
  4. The server software fulfills this request in whatever way is appropriate

(Note that SNI alters this process a bit, allowing server software to return different certificates when different hostnames are requested, but this doesn't help you. This only applies when you have multiple hostnames mapping to the same IP address, not the same hostname apparently referencing different machines.)

If you're absolutely sure that a different machine is responsible for the path https://www.company.com/some-path vs. https://www.company.com/, that doesn't mean you can directly connect to that machine to obtain its certificate. One can configure a reverse proxy like nginx to route requests for certain paths to different internal or even external servers, but in this case you are not communicating with that other server to get the content for https://www.company.com/some-path -- the server software for https://www.company.com/ is making this connection, presumably validating the other system's certificate if connecting over HTTPS, and then relaying the data to you. In this situation, you never directly connect to the other server, and even if it's publicly accessible, you have no way of knowing its hostname/IP address/anything about it.

e: and of course someone can set up https://www.company.com/some-path to redirect to a different hostname/path altogether, in which case it should be pretty trivial to parse that reply in your software to connect to that other host instead

Lysidas fucked around with this message at 22:34 on May 12, 2018

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

QuarkJets posted:

I agree, this seems convoluted and bad. I assume (hope?) that people here aren't using it that way

This specific case of "fixed path / string / string" does seem convoluted and bad, but I've never really seen anyone do that in practice.

I use pathlib very extensively, and I use the overloaded / operator extremely often. It's really nice when having a Path representing a user-supplied or dynamic directory and wanting to refer to some hardcoded filename in that directory.

pathlib became much nicer to use in 3.6 also, since builtin IO functions now support path-like objects in addition to string paths. This means you can now do:

Python code:
with open(some_path_object) as f:
    do_something_with(f)
instead of having to call methods on the Path object, like:

Python code:
with some_path_object.open() as f:
    do_something_with(f)

Adbot
ADBOT LOVES YOU

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
As time goes on, I'm getting less and less happy with the pyplot API in matplotlib. I'd prefer to use the object-oriented API in new code, but I always find it hard to prioritize learning a new API over creating the plot that I need at any given moment.

I remember seeing a document (maybe part of the matplotlib documentation) that was something like a side-by-side description of how to perform common operations with the two APIs. For example, "this is how you instantiate a figure, and the axes, and select the artist" for the OO method, vs. "call pyplot.figure" for the stateful Matlab-like API.

I'm having a hard time finding this, though; does anyone know of any documentation like this?

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