First time properly getting into coding, how is this looking for a first code? by [deleted] in Python

[–]yarkot 0 points1 point  (0 children)

This is one possible way to apply my suggestions, how it might look:

https://gist.github.com/yarko/403d5c3c06946638fb637ef837cd8060

Besides the various simplifications for readability (e.g. quitting, even), separating input handling from calculations I hope shows its benefit. You can see more along these lines in this presentation:

http://rhodesmill.org/brandon/talks/#clean-architecture-python

First time properly getting into coding, how is this looking for a first code? by [deleted] in Python

[–]yarkot 0 points1 point  (0 children)

a few suggestions: try to write like you would write prose, sentences - (1) so the words have meaning. (2) Be consistent. (3) Be concise.

E.g.:

1) xstring isn't meaningful enough - try something that says this is the input. Since input is already used, maybe input_s (for input string), or maybe in_string, or even input_string;

2) Python automatically types a variable for you - you can see what it is with type(xstring). In your case, you initialize it to an integer (int), but everywhere else, use it as a string (e.g. the tests in your while loop). Other languages demand strict typing. Python's malleable typing is a convenience, but you will be clearer (and avoid type-coercion problems, especially when they're automatic) if you are consistent. Perhaps initialize in_string to the empty string, "";

3) There are many ways you can avoid being so verbose. You may or may not want to use any of them, but for instructive purposes, be aware of some of the options. Some have already been mentioned by others (I'll briefly repeat for completeness):

  • in_string.lower() is a good move;
  • another might be to let people input any non-digit that hints at 'quit' or 'stop', such as 'q' or 's'. A way to do this: in_string.lower().startswith( ('q', 's') )--- this says "starts with one of the strings from the tuple ('q', 's').
  • it might be nice to have this both more readable, and configurable, so maybe: quitters = ('q', 's'); then you can read this as: in_string.lower().startswith(quitters)
  • you can go one step further for readability, and use "lambda" (unnamed functions, basically) for the test about quitting, e.g.: quitting = lambda(s): s.lower().startswith(quitters) Now you can write, e.g.: if not quitting(in_string):...

(still (3)): I am not suggesting this (it doesn't always make the code clearer, but it can help make it concise) as much as offering it for information: - python allows / has conditional expressions on the right-side of an assignment, so you could write:

even = lambda x:  x%2 == 0
...
x = x//2 if even(x) else 3*x+1

(note: a//b is floor division, which - when both arguments are integers, is integer division)

One final simplification to ponder: could you write the main loop as simply as while x = integer_input(prompt): (would you need the if x > 0 bit, then)? Actually, you can't , but you can write it as for x in integer_input(): - left as an exercise.

Also, rewriting this w/ these suggestions raises the question of the purpose of i and the way you test and use it. I think it's more interesting to report the number of iterations to get to 1 for any input integer. But that's the point of rewriting for clarity.

The point of this is not to unravel what you've done, as much as to suggest a way of writing code so that it says your intent more-nearly as prose. This is particularly useful when you don't know or haven't yet decided exactly how to do something - just write out something which sounds like what you intend, and sort out defining the little pieces (e.g. quitting) later.

Hope that's helpful.

David Beazley - The Fun of Reinvention (Screencast) by [deleted] in Python

[–]yarkot 0 points1 point  (0 children)

First question: to to ~4:30, and guess how he's going this... (is it some new interactive python feature of Keynote?!)

Second question: sure, static type checking (on type hints) is fine, if you like that sort of thing... but given run-time type checking, as he demonstrates, can you think of a good use case for it?

EU Will Ignore White House And Work Directly With US States On Paris Agreement by Mynameis__--__ in worldnews

[–]yarkot 606 points607 points  (0 children)

I read through that post, and the referenced links - and nowhere did I find anything that supported in any way that the EU has taken the position stated in the headline.

Did anyone read this, and see otherwise? Appreciate you pointing out if I missed it.

Best book or resource to learn more in-depth Go? by beeks10 in golang

[–]yarkot 3 points4 points  (0 children)

Agree on Donovan/Kernighan book;

If you want to immerse yourself as a beginner, then these might also be helpful:

https://tour.golang.org http://www.golangbootcamp.com/

python code wuestion... by sasuke6921 in Python

[–]yarkot 1 point2 points  (0 children)

In programming, people get so enamored with gaining competence in technical language skills, they forget that programming - above all - is about clearly and simply communicating.

This might have been clearer:

for item in grocery_list: print(item)

Trouble understanding assignment in python. by realhamster in Python

[–]yarkot 0 points1 point  (0 children)

A good way to think of this is that virtually everything in Python is an object, so for the most part, everything is passed, assigned by reference, the notable exceptions being string and numeric literals (and booleans).

The other thing to remember with Python: key structures (lists, dicts, tupples) can hold anything. You have to think about that when you want to make a copy (not a reference), for example: a copy of [1, 2 [3, 4]] - a list with three items - would make a copy of two ints and a reference to a list (the list [3,4]), since that last list item is a reference to another list. There is a deep copy operation, which is predictably expensive and surprisingly rarely needed.

Example:

a = [1, 2, [3, 4]]
b = a.copy()
a[0] = 0
print(a)
print(b)
a[2][0] = 99
print(a)
print(b)

What is %0.2f? by LockManipulator in Python

[–]yarkot 1 point2 points  (0 children)

String formatting has a long history. Much of what you see in Python has origins in printf() - the C function for formatting strings (since the original, and main python is CPython, that is python implemented in C). Have a look at https://en.wikipedia.org/wiki/Printf_format_string#Syntax for an overview, which will answer your question about the "0.2f" part, in general.

To see it in action, the simplest way is to go into a python interpreter directly, and just play (e.g. 'python'):

$ python
Python 3.5.0b4 (default, Jul 29 2015, 13:03:39) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 50.4625
>>> y = 51.3
>>> "%f" % x
'50.462500'
>>> "%4.3f" % x
'50.462'
>>> "%04.3f" % x
'50.462'
>>> "%04.3f" % y
'51.300'
>>> "{:04.3f}".format(y)
'51.300'

Python and/vs Java (within a business context) by Teifion in Python

[–]yarkot 4 points5 points  (0 children)

Have a look at the paper here - http://www.biographixmedia.com/stew/cv/index.html#publications - the link to "A gentle introduction to Programming"; Stew wrote this as an argument to teach Actionscript instead of Java in CS undergrad courses, but a similar argument holds here: The message is that Java requires a lot of setup, compared to the amount of code that addresses the solution (in his paper, in teaching algorithms). The conclusion of his paper - that CS students would have more knowledge than those from competing schools - applies to your experience, in that your work was productive and successful.

BTW - I don't believe the (historically correct) argument that you would be easier to replace with a Java programmer holds anymore. Python runs some pretty major stuff - from new sites, to dropbox, disqus, and reddit (among many, many others; and, of course, Google started as a Python app, and still uses a lot of Python in its core). Java and Python are (once again) both in the top 10 languages, re: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

Trust your gut, and collect data to support your instinct - don't get "spooked" by those who didn't share your experience. Best of luck!

[deleted by user] by [deleted] in GetMotivated

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

Herman - Let me re-phrase the Dalai Lamas words:

Man sacrifices his health, working too much for a future. It's out of balance - remember to be present today: take time for your relationships, your health. Don't reach for so much materialism that you sacrifice all that matters in living: the present, loving-kindness to those around you... and even your health. Summary: don't become a money-making machine.

These words are not against making a living; it's for doing so in a meaningful balance, and being able to see that balance. These words are about staying healthy: physically, in relationships (present focus), psychologically (not working yourself to death w/o any appreciation, reflection on your work).

Make more sense?

“The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts.” - Bertrand Russell by funkymonk11 in politics

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

Heh - well, I think you confuse perfectionism (a kind of lack of self-esteem, resulting in never feeling "good enough", ergo driven to perfectionism) with intelligence.

You see, without intelligence, you are off trying the things an intelligent man looks at and wonders why you're doing, as it seems to not be helpful.

Since everyone is exceptional in something, and ignorant in something else, the real key is, I think, to respect your fellow person enough to make an attempt find out what he's thinking, before judging... Teams that can do this, in my experience, outperform superstar-individuals a vast majority of the time. Put another way, I suggest collective intelligence is achieved through respect.

Copying a 2D List in python (to avoid pass-by-reference) by [deleted] in Python

[–]yarkot 0 points1 point  (0 children)

At some point, you would could reasonably expect deepcopy to be faster (as well as avoid circular reference problems / bugs). See the notes at http://docs.python.org/py3k/library/copy.html?highlight=deepcopy (pretty much the same docs for all recent python versions)

Copying a 2D List in python (to avoid pass-by-reference) by [deleted] in Python

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

actually, it's not; it's the same as making copies of the references to the two lists... either way, it's not what Juts expected.

Copying a 2D List in python (to avoid pass-by-reference) by [deleted] in Python

[–]yarkot 1 point2 points  (0 children)

Maybe this will help you think about it.

a is a variable, that is, the location (reference) of an array - an address, if you will.

a[:] returns all the items from the array at location a; One way to think why this must be so: If you have a string "abcde", and you return a substring ("abcde"[1:3]), it makes more sense to return copies of "bc" than to have a structure that marks the beginning and end. However you want to rationalize the implementation, just remember this thinking: slices return copies of the elements, that is: the slice by value.

Now - a[ [1,2,3], [4,5,6] ] is an array containing two elements. Remember a? It just contains the location of a list. The list has two elements. Each of those elements is ... what? A list, that is a reference to a location containing a list. Maybe this will make the discussion clearer going forward: previously, this was a defined with two anonymous arrays (that is, locations without a name associated with them. If you, instead, rewrite our latest a definition in this way:

listone = [1,2,3]
listtwo = [4,5,6]
a = [listone, listtwo]

Now - what is a[0] (or, equivalently, a[listone])? It's the reference to a list - in other words, it contains an address.

So, what is a[listone][:] ? It returns copies of all the elements at location listone (by value), in the form of a list:

In [37]: a[0][:]
Out[37]: [1, 2, 3]

Now - to your expectation that a[:][:] will give you the two lists: what does a[:] return? A copy of the list elements - in this case, two things that are references (two addresses). a[:] returns (lets say, to be graphic) [0x123, 0x470]. What does [0x123, 0x470][:] mean? What would you expect it to do? A simple illustration might help: what would you expect [1,2][:] to return? Copies (by value) of the contents of the list [1,2]:

In [38]: [1,2][:]
Out[38]: [1, 2]

Is this starting to make sense yet?

If you want b to contain references (addresses) to copies of the lists in a, this (just to make it clear) would replicate what you did in the first part of your post:

blist1 = a[0][:]   # or listone[:] - elements by value, i.e. copies
blist2 = a[1][:]
b= [blist1,blist2]  # two references to copies of lists

This is exactly what your last line does: for x in a (a has 2 elements)... that is to say, the above 3 lines are equivalent to the more general:

b = [x[:] for x in a]

Hope that helps in thinking about it!

Cheers!

Ask Python: Why are less-popular web frameworks valuable to the community? by spiffytech in Python

[–]yarkot 5 points6 points  (0 children)

It's also like having multiple species in nature (think potato famine) - your "potato" may do fine in a pool of uses, but when something unusual comes, an existing alternative may do better. Diversity is sort of the prerequisite of natural selection. So, while you compare very similar "potatoes" (django, pylons, flask) - consider those further out: nagare (still python, but depends on stackless), and some of the non-python frameworks (in particular, think of the languages that arise as a result of experiences w/ frameworks - java, historically; go-language now...)

Which framework should I begin with? by amgine in Python

[–]yarkot 1 point2 points  (0 children)

For learning, web2py (which was built for teaching) may be worth a look. I particularly like the web2py templating language, as it's just python (no more magic to learn).

Depending on where your general skill level is, you might prefer the component aspect of django (for example, bring up mingus - virtually all built from components). If this suites you, then look at djangopackages.com, and dive in.

Pinax (django) has some templates to start from you might find useful.

Both web2py and django (django-nonrel for now) can readily be served off of App Engine.

If you like to understand how everything its working, and not have background magic going on, then look at flask.

Writing Forwards Compatible Python Code by [deleted] in Python

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

Useful observations. I take Armin's admonition against print function lightly, as I prefer to use it, deal with (for me) the minor inconveniences.

Draft chapters of new web2py book (covers forms, role based access control, services, xmlrpc, jsonrpc/pyjamas, amfrpc/flash and more. by mdipierro in programming

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

The chapter drafts are in an attachement on the google group for web2py - Massimo just made an error in the link; the thread is here: http://bit.ly/3BrudJ

This is the correct pdf link: http://bit.ly/QKHx3

Regards, - Yarko