arisuchan    [ tech / cult / art ]   [ λ / Δ ]   [ psy ]   [ ru ]   [ random ]   [ meta ]   [ all ]    info / stickers     temporarily disabledtemporarily disabled

/λ/ - programming

structure and interpretation of computer programs.

formatting options

Password (For file deletion.)

Help me fix this shit.

Kalyx ######

File: 1492464152227.png (90.3 KB, 512x512, 1492029822151.png)


Share resources, PDFs and books. Also discuss and share projects you have been working on.

Beginner Questions are also welcome.


File: 1492464186101.pdf (2.56 MB, Learn Python The Hard Way ….pdf)


i think this is for python2. Not that the difference is that big, but some things like input are different


File: 1492511645092.pdf (18.55 MB, John V. Guttag - Introduct….PDF)

Introduction to Computation and Programming Using Python, Second Edition
With Application to Understanding Data
by John V. Guttag

>i think this is for python2. Not that the difference is that big, but some things like input are different
I think that this book is designed for new learners that it is pretty important to make sure they're using the latest edition of the book. The second edition switched to Python 3.


File: 1497359214409.jpg (27.66 KB, 250x367, 9da8e6ba78759e88180d414450….jpg)

Working on an XKCD browser as one of my first GUI projects as well as my first standalone project.
So far, I worked out displaying an image using pygtk and loads of trial and error. Have yet to add the ability to go to the first or last comic and actually navigating them.

I'm also trying to make a scraper framework for fun. It started with me wanting to scrape a school textbook but now I'm rewriting it so people can import it as a function.


I'm trying to learn about web-scraping in general by messing around with, without very much prior experience with python. How would I list the amount of items in a set like this-
 pagecount = tree.xpath('//div[@class="pages"]/a[@href]/text()') 

The full script is
from lxml import html
import requests
page = requests.get('')
tree = html.fromstring(page.content)
pagecount = tree.xpath('//div[@class="pages"]/a[@href]/text()')
print pagecount

I tried len(), but it didn't work, so I assume this is different from a normal array?


What was the error message? It's a list so
should work. You can check the type with


It doesn't return an error message. It doesn't return anything at all. Do i need to have "print" first?



i'm not >381 but you need parens around pagecount in the print statement. len(pagecount) should give you its length as previously stated. in that code, it should be a list.


Print works both with and without parenthesis. Len doesn't return anything, regardless of parenthesis


What did you try? Like this?
from lxml import html
import requests
page = requests.get('')
tree = html.fromstring(page.content)
pagecount = tree.xpath('//div[@class="pages"]/a[@href]/text()')

It prints 5 for me.


The print statement works with or without parenthesis. len(pagecont) doesn't return anything.


Sorry for the double post, It looked like my post had been deleted on my end.
That works, it returns 5. I think I misunderstood how len() works, and assumed that it would return a number on its own without needed a print command. Thank you for the help


Returned values are not printed by default. The behavior is different in interactive REPLs though where it's assumed that you want to see what's returned.


You can also use
and use CSS like syntax, e.g.

cssselect('div.pages a[href]').text


When is Python insufficient?

I find Python faster to code and easier to debug than most languages,
but sometimes i feel like it won't run fast enough.

How do you decide when Python will be good?
I always feel like i'm wasting my time using other languages when Python might be able to be just as fast,
especially if i could use some library written in c.


File: 1508165253915.png (9.99 KB, 183x224, 8051d424d71f9e8f244d38b5cf….png)

What's a good python debugging solution?
I do most my py scriptkiddie soykaf in Sublime, but most my debugging come out of print() and exception catching


File: 1508218572574.jpg (25.78 KB, 384x272, fedora_brand_pasta.jpg)

I found the print function and the stack traceback have made it easy to find where and why my code is not working as it's supposed to.

What are you having problems with?


That's kind of the point.

You optimise your bottlenecks. That's one of the benefits of proper function separation and library interfaces.

A new import (from a C or other object-code library), a minor refactor if you didn't use the same function names and you're back in business.

You'll probably never reach the time python glue-code itself ends up too slow. And by then you could probably switch over to CLisp w./SBCL to squeeze a few fractions of a second more.

There's decent python debugging in a few emacs major-modes.


Also, the learning the hardway series is notorious for not explaining concepts properly and the few explanations he makes, ain't very good.


> How do you decide when Python will be good?

My general rule of thumb is if it is something that will be run infrequently as say a single command or script then python makes sense as it is quick to write whatever I need. Anything I expect to be run thousands of times regularly or running constantly in the background I'll pick up C or Go instead.


what makes you feel like this is a good rule of thumb?


Probably nothing at all. CPython is a bad choice if you're going to be spinning up the runtime over and over, but Python is perfectly fine running in the background.


This is exactly how I use it, when I need to write quick throwaway scripts or other really simple things, I just write it in python. All the soykaf I used to do in perl and batch, I just write in python now. I wouldn't use it for any actual component of some real software though.


Python is really good for statistics, analysis, machine learning and simple AI, what is more you can use python for web development


I have found this to be true as well, but i was wondering what python would not be good for.



I'm currently developing a program of scientific research in python. I found python very convenient with its extensive pre-made libraries including its convenient math lib and tkinter(GTK and Qt is now a pain in the ass) which is exactly what i'm looking for for ages. It's not something state of the art professional level, just something i will practice on, so it doesn't really matter that it's magnitudes less resource-effective than something like C.


Does Python have a healthier way of doing this?

In = {
        ' ': assign(),
        'a': assign(),
        'b': assign(),
        'c': assign()

out = {
        In[' ']: ' ',
        In['a']: 'a',
        In['b']: 'b',
        In['c']: 'c'


Probably. I have no idea what you're trying to do though.


I need a dictionary with each letter of the alphabet containing a value representing the same, and another dictionary with the values of the first dictionary containing the letters of the first. Sounds stupid, it was the first thing I thought of when writing code


If I'm understanding you, you want a bidirectional map. To do this properly you need to maintain two separate mappings. You can juggle two dictionaries or you can use a robust abstraction:


Actually, I want each character (not necessarily all) to be put into a dict without writing everything manually, and the second dict (out) contains the values of the first dict (In) characters along with each character, as if it were a for directed to dicts.


Different Alice.
def value_for_char (ch):
   return ...   # unique values

my_chars = "..."

chars_to_values = {
   ch: value_for_char (ch)
   for ch in my_chars

values_to_chars = {
   v: k
   for k, v in chars_to_values.items ()


Thank you, I did not know that could do this with a dictionary.


>share projects you have been working on
Made a screenshot tool for linux using pyqt, just added multi-monitor support. Has custom uploads and cli. May segfault randomly and also eats soykafton of ram.
Pretty much garbage but I'm fine with it.



scientific computing in python can be about as resource-effective as c if you can figure out how to vectorize the difficult parts.


>scientific computing in python can be about as resource-effective as c if you can figure out how to vectorize the difficult parts.
That's because the libraries you're using are themselves written in C.


Currently learning Python (first programming language) and having a really good time. Right now I'm learning about for loops and ranges. I was hoping someone could explain this phenomena to me.

for number in range(5):
    print (range(number))

[0, 1]
[0, 1, 2]
[0, 1, 2, 3]

My question is why is the first slot blank? Why is it not [0] on the first one, and the last one [0, 1, 2, 3, 4]?


you get the positive integers that are smaller than
. The first is empty since there's no such number (0 is not smaller than 0), and
[0, 1, 2, 3, 4]
is not included as
will never be 5 (as it is not smaller than 5).


The upper limit of 'range' is exclusive. The first 'number' is 0, and a range with an exclusive upper limit of 0 is empty. The last 'number' is 4, and you get the range with an exclusive upper limit of 4.

More importantly, drop whatever it is you're following and learn Python 3 instead.


Yes, I realize I've been running them under python2 instead of 3.
instead of

Luckily I believe what I've learned has been python3, so my learning is more or less up to date. I appreciate the concern.



[Return] [Go to top] [ Catalog ] [Post a Reply]
Delete Post [ ]