File IO and pictures

Reply

Join Date: Jun 2008
Posts: 46
Reputation: Fuse is an unknown quantity at this point 
Solved Threads: 3
Fuse's Avatar
Fuse Fuse is offline Offline
Light Poster

File IO and pictures

 
0
  #1
Jun 5th, 2008
Hi there. I'm new to Python. I started learning it 3 days ago. I've figured out everything by myself so far, but this is stumping me:

Whenever I load a file such as a html document or a text file, it loads fine. But when I load this gif or png file (probably many other formats, too), it only loads the first few bits of it. It happens in both urlopen() and normal file open().

E.g. http://www.majhost.com/gallery/Lijik...-1/ewjclay.png

Only loads:
  1. ‰PNG
  2. 

Here is my code. It's a programme I made (the first one I've ever made, actually - in the past I always lost interest half-way) to help people learn regular expressions (because when I was learning them yesterday I found it a hassle using the Python interpreter). It's helpful for anybody using regexps, though, I reckon, because it's a great testing ground. The other programme I'm making downloads all the images from a website by applying a regexp to the HTML code and then forming absolute links to the images and putting them in a set to remove duplicates. It's currently just a script but tomorrow I'll make it a programme where you can preview the images and select which ones to save to your computer.

Also, any comments on my coding style - what I'm doing wrong, what I'm doing inefficiently, etc, would be appreciated.

Thanks!

  1. import urllib
  2. import re
  3. import wx
  4.  
  5. # Welcome to the Regular Express!
  6. # Code by Fuse
  7.  
  8. # source = file name or URL
  9. # contents = what's inside the file loaded
  10. # patternArea = what is the regexp to use?
  11. # looadArea = what is the url or file to load?
  12.  
  13. FRAME_WIDTH = 500
  14. FRAME_HEIGHT = 600
  15.  
  16. class MainFrame(wx.Frame):
  17.  
  18. def __init__(self):
  19. wx.Frame.__init__(self, None, title = 'The Regular Express',
  20. pos = (200, 75), size=(FRAME_WIDTH, FRAME_HEIGHT))
  21.  
  22. self.background = wx.Panel(self)
  23.  
  24. self.infoBtn = wx.Button(self.background, label = 'Info')
  25. self.infoBtn.Bind(wx.EVT_BUTTON, self.infoFunction)
  26.  
  27. self.clearBtn = wx.Button(self.background, label = 'Clear')
  28. self.clearBtn.Bind(wx.EVT_BUTTON, self.clearArea)
  29.  
  30. self.loadBtn = wx.Button(self.background, label = 'Load')
  31. self.loadBtn.Bind(wx.EVT_BUTTON, self.loadFile)
  32.  
  33. self.goBtn = wx.Button(self.background, label = 'Go!')
  34. self.goBtn.Bind(wx.EVT_BUTTON, self.patternMatching)
  35.  
  36. self.inputArea = wx.TextCtrl(self.background, style = wx.TE_MULTILINE)
  37. self.loadArea = wx.TextCtrl(self.background)
  38. self.patternArea = wx.TextCtrl(self.background)
  39. self.outputArea = wx.TextCtrl(self.background, style = wx.TE_MULTILINE | wx.TE_READONLY)
  40.  
  41. self.inputArea.SetValue('Enter the data you want to parse into this area. Alternatively, press load and select a file instead.')
  42. self.outputArea.SetValue('The data will appear here once it has been parsed.')
  43. self.patternArea.SetValue("Enter your regular expression here.")
  44. self.loadArea.SetValue("Enter file location here.")
  45.  
  46. self.horBoxOne = wx.BoxSizer()
  47. self.horBoxOne.Add(self.patternArea, proportion = 1, flag = wx.EXPAND)
  48. self.horBoxOne.Add(self.goBtn, proportion = 0, flag = wx.LEFT, border = 0)
  49. self.horBoxOne.Add(self.infoBtn, proportion = 0, flag = wx.LEFT, border = 0)
  50.  
  51. self.horBoxTwo = wx.BoxSizer()
  52. self.horBoxTwo.Add(self.loadArea, proportion = 1, flag = wx.EXPAND)
  53. self.horBoxTwo.Add(self.loadBtn, proportion = 0, flag = wx.LEFT, border = 0)
  54. self.horBoxTwo.Add(self.clearBtn, proportion = 0, flag = wx.LEFT, border = 0)
  55.  
  56. self.verBox = wx.BoxSizer(wx.VERTICAL)
  57. self.verBox.Add(self.inputArea, proportion = 1, flag = wx.EXPAND, border = 5)
  58. self.verBox.Add(self.horBoxTwo, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 0)
  59. self.verBox.Add(self.horBoxOne, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 0)
  60. self.verBox.Add(self.outputArea, proportion = 2, flag = wx.EXPAND, border = 5)
  61.  
  62. self.background.SetSizer(self.verBox)
  63. self.Show()
  64.  
  65.  
  66. def loadFile(self, event):
  67. source = self.loadArea.GetValue()
  68. if re.search(r'^http://.*?', source) != None:
  69. target = urllib.urlopen(source)
  70. else:
  71. target = open(source, 'rb')
  72. self.inputArea.SetValue(target.read())
  73. target.close()
  74.  
  75. def patternMatching(self, event):
  76. matchList = re.findall(self.patternArea.GetValue(), self.inputArea.GetValue())
  77. self.outputArea.SetValue('\n'.join(['%s' % v for v in matchList]))
  78.  
  79. def clearArea(self, event):
  80. self.inputArea.SetValue('')
  81.  
  82. def infoFunction(self, event):
  83. self.outputArea.SetValue(r"""Regular Expression Syntax:
  84.  
  85. . means all characters except the newline character '\n'
  86. Example: "H.llo" would match "Hello" as well as "H llo"
  87. Note: if you wish to type the actual '.' character, type '\.' instead. This applies to '^', '$', '*', etc aswell.
  88. Note: alternatively, you can type [.] or [^] etc. More on that below.
  89.  
  90. ^ matches the start of the string.
  91. Example: "Hello" would match "Hello" in "Hello, soldier." but not "He said 'Hello' to me."
  92.  
  93. $ matches the end of the string. Similar to '^'.
  94. Example: "bye$" would match "bye" in "Goodbye" but not "Goodbye."
  95.  
  96. * matches the preceding regexp 0 or more times.
  97. Example: "Hello*" matches "Hello", "Helloooo" and "Hell".
  98. Example: "H.*" matches "Hello", "Hello!", "Hell", "Hi" and even "H".
  99.  
  100. ? matches the preceding regexp 0 to 1 times.
  101. Example: "Hello?" matches "Helloooo" up to "Hello".
  102. Note: '?' is also used to enable and disable greediness. More on that below.
  103.  
  104. *?, +?, ?? and greediness
  105. Taken from the Python documentation:
  106. "The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn't desired; if the RE <.*> is matched against '<H1>title</H1>', it will match the entire string, and not just '<H1>'. Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using .*? in the previous expression will match only '<H1>'."
  107.  
  108. Note to self: type up more tomorrow.""")
  109.  
  110. app = wx.App()
  111. window = MainFrame()
  112. app.MainLoop()
  113.  

The other programme I'm making downloads all the images from a website by applying a regexp to the HTML code (the easy part) and then forming absolute links to the images (the hard part) and putting them in a set to remove duplicates. It's currently just a script but tomorrow I'll add a GUI where you can preview the images and select which ones to save to your computer. If anybody is interested, I can post the code for that tomorrow when I finish?

Oh, and does anybody know how to get an explorer-like view for loading files in wxPython (as in any normal Windows app)? I could use one here, instead of having to type in the relative or absolute file location manually.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 977
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 273
woooee woooee is offline Offline
Posting Shark

Re: File IO and pictures

 
0
  #2
Jun 5th, 2008
You want to search for wxpyton file dialog
http://www.wxpython.org/docs/api/wx....log-class.html
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 46
Reputation: Fuse is an unknown quantity at this point 
Solved Threads: 3
Fuse's Avatar
Fuse Fuse is offline Offline
Light Poster

Re: File IO and pictures

 
0
  #3
Jun 6th, 2008
Cheers mate! That's ace.

Sorry if the OP is a jumble; I haven't had sleep. (I repeated myself about the image grabber script.)
Mir's Fuselage

Because what's not to like about being killed by a toilet seat from Mir's atmospheric re-entry?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 46
Reputation: Fuse is an unknown quantity at this point 
Solved Threads: 3
Fuse's Avatar
Fuse Fuse is offline Offline
Light Poster

Re: File IO and pictures

 
0
  #4
Jun 7th, 2008
I discovered what the problem was, and I will not (can't) fix it. It is the chr(0) character.

http://python-forum.org/pythonforum/...9bff2ac#p38443
Mir's Fuselage

Because what's not to like about being killed by a toilet seat from Mir's atmospheric re-entry?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 977
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 273
woooee woooee is offline Offline
Posting Shark

Re: File IO and pictures

 
0
  #5
Jun 7th, 2008
If you just want to display the pictures, use the Python Imaging Library (PIL). http://www.pythonware.com/library/pi...book/index.htm
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC