[Tutor] python equivalents for perl list operators?

2016-04-23 Thread Malcolm Herbert
hey folks - I've been a long time perl programmer and only recently
tried my hand a python, so it's probable that these questions are
non-sensical in this context but for the moment I'm trying to stay
afloat

I've been dabbling a bit with some lists and trying to work out how best
to abitrarily sort and filter these. Perl has a number of operators that
help with this in map(), grep() and sort() as follows:

  @raw = (2, 1, 4, 3);
  @grepped = grep { $_ >= 3 } @raw; # (4, 3)
  @mapped = map { $_ + 1 } @raw; # (3, 2, 5, 4)
  @sorted = sort { $a > $b } @raw; # (1, 2, 3, 4)

in this case:

grep() will return all list items for which the code block returns true

map() will return all list items as modified by the code block

sort() will return a sorted list of items, using the code block to
compare them (where $a and $b represent two items to be compared)

so - I've been able to at least work out the map() case above with a
list comprehension

  raw = [2, 1, 4, 3]
  mapped = [ x + 1 for x in raw] # [3, 2, 5, 4]

and I know that .sorted() would do what I want in this limited example,
but I'm after the ability to put abitrary code in here to determine
sort order or test an item for filtering (because the items they're
testing may be complex structures rather than these simple integers, for
example)

these seem so useful things to want to do that I'd imagine they're
probably a basic part of the language, but so far I've not seen anything
that might cover them with the exeption of map() as above - I am slowly
trawling my way through Learning Python (5ed) so I might yet get to
something related, I don't know

does anyone have any pointers?  ta

Regards,
Malcolm

-- 
Malcolm Herbert
m...@mjch.net
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does using "window.addchr()" to place a character at the lower right corner raise an exception?

2019-02-28 Thread Malcolm Herbert
yes - usually that was the prompt for the terminal to insert a new line ... on 
some terminals this is a behaviour you can't control as it's implemented in 
hardware, so curses fakes things by moving the cursor back to where it "should" 
be afterward.

If you try and to something when the cursor is at the extreme bottom right of 
the window/region and the terminal is hard-coded to do $magic immediately 
after, then your screen will be all messed up.

-- 
Malcolm Herbert
m...@mjch.net
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to Scrape Text from PDFs

2019-06-19 Thread Malcolm Herbert
This isn't  a response that's python-related, sorry, I'm still learning python 
myself, but more questions around the nature of the PDF and where I might start 
looking to solve the problem, were it mine.

The URLs that you are intending to match - are they themselves clickable when 
you open the PDF in another reader?  If so, then you might have better luck 
looking for the PDF element that provides that capability rather than trying to 
text-scrape to recover them.

Although unlikely inside a URL, text in a PDF can be laid out on the page in a 
completely arbitrary manner and to properly do PDF-to-text conversion you may 
need to track position on the page for each glyph as well as the font mapping 
vector - a glyph of an 'A' for instance might not actually be mapped to the 
ASCII/Unicode for 'A' ... all of which can make this a complete nightmare for 
the unwary.

So - when I last looked at generating a PDF with a live link element, this was 
implemented as blue underlined text (to make it look like a link) with an 
invisible box placed over the top which contained the PDF magic to make that do 
what I wanted when the user clicked on it.

I would suspect that what you might want would be a Python library that can 
pull apart a PDF into it's structural elements and then hunt through there for 
the appropriate "URL box" or whatever it's called ...

Hope that helps,
Malcolm

-- 
Malcolm Herbert
m...@mjch.net
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor