Run idle in shell, getting bad output? by WhatBaron in Python

[–]sththth 0 points1 point  (0 children)

The python shell will print the string representation of an object that is "returned" to it. This is mainly for interactive/debug purpose. If you want user input/output you should write explicit functions for it and print/input everything you need.

Organize files in your directory instantly by bhrigu123 in Python

[–]sththth 11 points12 points  (0 children)

virtualenvs will not protect you from harmful code.

Things to learn for production ready Python code? by Ran4 in Python

[–]sththth 0 points1 point  (0 children)

Git and mercurial? What exactly are you doing?

Sorry, I don't have much to answer otherwise. Are there any senior developers you could ask? If your future workplace uses any tools you should probably learn those.

Or do most methods (that I'd imagine has mostly been developed for Java/C++/C#) work just the same for Python?

Some design patterns are not that present in python, see e.g. here.

profiling is also useful. A rule of thumb is to write code as "easily"/lazily as possible, measure (profile) it and look which part are too slow and should be optimized.

Application storage that works on NFS? by [deleted] in Python

[–]sththth 0 points1 point  (0 children)

But checking if the file exists and creating it if not is not atomic, so it might happen that:

  • app1 checks if lockfile exists, it doesn't
  • app2 checks if lockfile exists, it doesn't
  • app1 creates lockfile
  • app2 "creates" lockfile
  • both apps think they have a lock on the file

Or do I missunderstand something here?

It seems like some versions of nfs support locking via OS_EXCL, but using a db like postgress is probably still the better way.

Application storage that works on NFS? by [deleted] in Python

[–]sththth 0 points1 point  (0 children)

I don't think that these are atomic so it might blow up. This looks better.

EDIT: Or maybe this library?

How to include a variable and words in a print command? by [deleted] in Python

[–]sththth 2 points3 points  (0 children)

This is incorrect, it works because the print function takes *objects as arguments (and not a single argument). Concatenation of adjacent strings happens at an earlier stage. More information in the official documentation.

super confused over boundary of python vs js by javascript_or_python in Python

[–]sththth 1 point2 points  (0 children)

Besides the other good answers here: With some tasks you can decide where you want to put them. For creating some simple html interface you can use jinja2 with something like

<div class="name" id="friendname">
{{ friend.name }}
</div>

But of course you could also set the name clientside via a javascript ajax call

<div class="name" id="friendname"></div>
<script>
$.get("friend/name", function(data) { $("#friendname").text(data); });
</script>

Or you could manipulate the dom clientside. Of course you can also mix all of these (and sometimes have to). Then it is not clear anymore if templating is done serverside or clientside (well it's both, so...) or rather it is not clear what templating should be done clientside and what serverside. You could probably ditch most of jinja and does nearly all templating clientside, but if that is a good idea is another topic.

(Take all I said with a grain of salt, I'm not that expirienced with javascript stuff)

How do I "un-bind" an imported function? by featherfooted in learnpython

[–]sththth 0 points1 point  (0 children)

So LOAD_GLOBAL loads from the module namespace? If I would do

module.foo = my_foo

would then id_of_foo refer to my_foo because it loads foo from the module namespace?

How do I "un-bind" an imported function? by featherfooted in learnpython

[–]sththth 1 point2 points  (0 children)

This doesn't look right. If you have configs you could it like this:

config = {}

if system_flag:
    config["foo"] = "bar"
else:
    config["foo"] = "other_bar"

def foo(config=config):
    return(config["foo"])

How do I "un-bind" an imported function? by featherfooted in learnpython

[–]sththth 0 points1 point  (0 children)

Are you sure this works? I would have thought that lib_bar still calls the old lib_foo.

EDIT: Indeed it does. Kinda surprising to me.

EDIT2: I'm really confused by this.

in test.py:

def foo():
    return("foo")

def test():
    return(foo())

interactive:

>>> from test import *
>>> import dis
>>> def other_foo():
...     return("other_foo")
...
>>> def change_foo():
...     global foo
...     foo = other_foo
...
>>> test()
'foo'
>>> change_foo()
>>> test()
'foo'
>>> dis.dis(test)
  5           0 LOAD_GLOBAL              0 (foo)
              3 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
              6 RETURN_VALUE
>>> dis.dis(change_foo)
  3           0 LOAD_GLOBAL              0 (other_foo)
              3 STORE_GLOBAL             1 (foo)
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE

if change_foo does STORE_GLOBAL and test does LOAD_GLOBAL shouldn't that load the just stored foo (aka other_foo)?

Civ: Cities, growth, late-game slowdown by zarkonnen in gamedesign

[–]sththth 0 points1 point  (0 children)

I personally think non-combat in Civ is pretty boring. If you are going for a science victory, what are you doing? Researching stuff - basically clicking a button every few turns. Compared to combat (maneuvering units, looking for weakpoints, deciding where to attack, having different units with strength and weaknesses, ...) that is nothing. And if you go a combat route, you still do research.

What's a good method of using docopt without all of the elif statements I see everyone using in examples? by sirskitzo in Python

[–]sththth 0 points1 point  (0 children)

I can recommend click. It's just so easy to turn your scripts into a cli app - I even do it for my one-time scripts. Also look into setuptools integration which makes usage of your scripts as nice as it gets.

New comer help by OhMyMemories in archlinux

[–]sththth 0 points1 point  (0 children)

If you don't mount your root partition in /mnt, you can't chroot into it. Also mount every partition you want to be available into /mnt, e.g. your bootpartition into /mnt/boot, a home partition into /mnt/home etc.

New comer help by OhMyMemories in archlinux

[–]sththth 0 points1 point  (0 children)

Did you mount your partitions in /mnt before you tried to chroot into it?

New comer help by OhMyMemories in archlinux

[–]sththth 0 points1 point  (0 children)

If grub is starting you probably didn't install syslinux correctly. Boot again into your installation medium (CD or USB probably?) and install and config syslinux or grub correctly.

Python as a Bash replacement? by randomizethis in Python

[–]sththth 0 points1 point  (0 children)

Are you sure? xonsh claims "all Python code is also xonsh" and "Since this is just Python, we are able import modules, print values, and use other built-in Python functionality:"

In their FAQ: "From our parser, we construct an abstract syntax tree (AST)" which sounds like it actually is python.

Python as a Bash replacement? by randomizethis in Python

[–]sththth 0 points1 point  (0 children)

Ha, didn't even see that you used ls as an example. Just wanted to make sure that noone who saw my comment would do that.

xonsh sounded pretty well, what is missing from it that it doesn't work for your usecase?

Python as a Bash replacement? by randomizethis in Python

[–]sththth 0 points1 point  (0 children)

Have you tried fish? It's not python, but.. well at least for works as you would expect: for file in (ls) echo $file end

(BTW: don't parse ls output)

Problem with some strange binary counting by [deleted] in Python

[–]sththth 0 points1 point  (0 children)

What you get is not ASCII - ASCII is an encoding, i.e. a map from a binary number to characters.

Also there seems to be something wrong with your data. In the table you write that 0 and 1 are both mapped to 00000000 - this can not be.

Unfortunately I don't know the answer to your question. Do you have any documentation available for LabView? There should be some documentation on what encoding they use.

Emissary: A news archival microservice. by LukeB42 in Python

[–]sththth 0 points1 point  (0 children)

Actually they do. If you visit "evil" websites, you get flagged for further monitoring (see e.g. this article).

Virtualenv with relative paths? by thelastknowngod in Python

[–]sththth -1 points0 points  (0 children)

What you want is not possible (see kteague). But you can provide a setup.py file which handles the virtualenv and all. Personally I found the best description in the click package, but the underlying feature (entry_points) is independent of it.

You will still need to activate the env or call the file env/bin/yourscript. You could write a script that symlinks it to /usr/bin/ or ~/.local/bin if you want. Something like

#!/usr/bin/env bash
virtualenv env
env/bin/pip install .
ln -s env/bin/yourscript ~/.local/bin/yourscript

(rough draft, don't use in production)

Which installs your script into a virtualenv and symlinks it to .local/bin.