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

/λ/ - programming

structure and interpretation of computer programs.
Name
Email
Subject
Comment

formatting options

File
Password (For file deletion.)

Help me fix this shit. https://archive.arisuchan.jp/q/res/2703.html#2703

Kalyx ######


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

 No.8

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

Beginner Questions are also welcome.

 No.9

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


 No.15

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

 No.16

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
https://mitpress.mit.edu/books/introduction-computation-and-programming-using-python-1

>>15
>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.

 No.220

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.

 No.380

I'm trying to learn about web-scraping in general by messing around with lainchan.jp, 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('https://lainchan.jp/cult/')
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?

 No.381

>>380
What was the error message? It's a list so
len(pagecount)
should work. You can check the type with
type(pagecount)
.

 No.382

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

 No.384

>382

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.

 No.385

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

 No.386

>>385
What did you try? Like this?
from lxml import html
import requests
page = requests.get('https://lainchan.jp/cult/')
tree = html.fromstring(page.content)
pagecount = tree.xpath('//div[@class="pages"]/a[@href]/text()')
print(len(pagecount))

It prints 5 for me.

 No.387

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

 No.388

>>387
Sorry for the double post, It looked like my post had been deleted on my end.
>>386
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

 No.389

>>388
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.

 No.410

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

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

 No.793

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.

 No.798

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

>>793
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

 No.805

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

>>798
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?

 No.808

>>793
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.

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

 No.1547

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

 No.1548

>>793
> 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.

 No.1577

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

 No.1578

>>1577
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.

 No.1579

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.

 No.1580

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

 No.1582

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

 No.1583

>>1582
>>1580

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.

 No.1588

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'
}

 No.1589

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

 No.1590

>>1589
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

 No.1591

>>1590
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:

https://bidict.readthedocs.io/en/latest/intro.html

 No.1592

>>1591
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.

 No.1593

>>1588
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 ()
}

https://docs.python.org/3/tutorial/datastructures.html#dictionaries
https://docs.python.org/3/reference/expressions.html#dictionary-displays

 No.1594

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

 No.1595

>>8
>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.
https://github.com/sertraline/chizuhoru/

 No.1598

>>1583

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

 No.1600

>>1598
>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.
https://github.com/numpy/numpy

 No.1703

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.

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


Output:
[]
[0]
[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]?

 No.1704

>>1703
With
range(n)
you get the positive integers that are smaller than
n
. 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
number
will never be 5 (as it is not smaller than 5).

 No.1705

>>1703
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.

 No.1706

>>1705
Yes, I realize I've been running them under python2 instead of 3.
(ie,
python xxx.py
instead of
python3 xxx.py

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

 No.1739

test



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