stringio

Ruby Mock a file with StringIO

倖福魔咒の 提交于 2019-12-10 09:26:21
问题 I am trying to mock file read with the help of StringIO in Ruby. The following is my test and next to that is my method in the main class. def test_get_symbols_from_StringIO_file s = StringIO.new("YHOO,141414") assert_equal(["YHOO,141414"], s.readlines) end def get_symbols_from_file (file_name) IO.readlines(file_name, ',') end I want to know if this is the way we mock the file read and also I would like to know if there is some other method to mock the method in the class rather than doing

Serving Excel(xlsx) file to the user for download in Django(Python)

十年热恋 提交于 2019-12-09 17:35:09
问题 I'm trying create and serve excel files using Django. I have a jar file which gets parameters and produces an excel file according to parameters and it works with no problem. But when i'm trying to get the produced file and serve it to the user for download the file comes out broken. It has 0kb size. This is the code piece I'm using for excel generation and serving. def generateExcel(request,id): if os.path.exists('./%s_Report.xlsx' % id): excel = open("%s_Report.xlsx" % id, "r") output =

Convert PILLOW image into StringIO

折月煮酒 提交于 2019-12-09 05:39:41
问题 I'm writing a program which can receive images in a variety of common image formats but needs to examine them all in one consistent format. It doesn't really matter what image format, mainly just that all of them are the same. Since I need to convert the image format and then continue working with the image, I don't want to save it to disk; just convert it and continue on. Here's my attempt using StringIO: image = Image.open(cStringIO.StringIO(raw_image)).convert("RGB") cimage = cStringIO

python json dump writability “not write able”

别来无恙 提交于 2019-12-08 05:57:33
问题 So this is the second piece of a problem with my program, but a completely different issue, thanks to the helpful person who suggested JSON as a better method to do what I wanted.... Anyway... some success with JSON. The program also changed theme, I definitely am not tying to make a game, just get the inspiration to learn more about the concept of "saving" in python.. so here's my code so far, with a valid JSON coded file to read from.. but I ran into another snag, it reports this error when

python json dump writability “not write able”

独自空忆成欢 提交于 2019-12-07 13:55:29
So this is the second piece of a problem with my program, but a completely different issue, thanks to the helpful person who suggested JSON as a better method to do what I wanted.... Anyway... some success with JSON. The program also changed theme, I definitely am not tying to make a game, just get the inspiration to learn more about the concept of "saving" in python.. so here's my code so far, with a valid JSON coded file to read from.. but I ran into another snag, it reports this error when I try to use the JSON 's .dump method Error: Traceback (most recent call last): File "<string>", line

Strange “BadZipfile: Bad CRC-32” problem

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 06:14:06
问题 This code is simplification of code in a Django app that receives an uploaded zip file via HTTP multi-part POST and does read-only processing of the data inside: #!/usr/bin/env python import csv, sys, StringIO, traceback, zipfile try: import io except ImportError: sys.stderr.write('Could not import the `io` module.\n') def get_zip_file(filename, method): if method == 'direct': return zipfile.ZipFile(filename) elif method == 'StringIO': data = file(filename).read() return zipfile.ZipFile

Why is StringIO object slower than real file object?

拥有回忆 提交于 2019-12-07 03:13:25
问题 I'm looking through the source of StringIO where it says says some notes: Using a real file is often faster (but less convenient) . There's also a much faster implementation in C, called cStringIO , but it's not subclassable. StringIO just like a memory file object, why is it slower than real file object? 回答1: Python's file handling is implemented entirely in C. This means that it's quite fast (at least in the same order of magnitude as native C code). The StringIO library, however, is

Python, write in memory zip to file

孤街浪徒 提交于 2019-12-06 17:55:06
问题 How do I write an in memory zipfile to a file? # Create in memory zip and add files zf = zipfile.ZipFile(StringIO.StringIO(), mode='w',compression=zipfile.ZIP_DEFLATED) zf.writestr('file1.txt', "hi") zf.writestr('file2.txt', "hi") # Need to write it out f = file("C:/path/my_zip.zip", "w") f.write(zf) # what to do here? Also tried f.write(zf.read()) f.close() zf.close() 回答1: StringIO.getvalue return content of StringIO : >>> import StringIO >>> f = StringIO.StringIO() >>> f.write('asdf') >>> f

How can I get the response body from pycurl multi curl requests

本秂侑毒 提交于 2019-12-06 13:04:01
I am unable to get anything but empty responses when performing curl multi requests. No exceptions are thrown, but the response value has no content (commented in the below snippet) Here's a simplified version of my code: from StringIO import StringIO import pycurl class CurlStream(object): curl_count = 0 curl_storage = [] def __init__(self): self.curl_multi = pycurl.CurlMulti() def add_request(self, request, post_fields=None): self.curl_count += 1 curl = self._create_curl(request, post_fields) self.curl_multi.add_handle(curl) def perform(self): while self.curl_count: while True: response,

Python pandas NameError: StringIO is not defined

爷,独闯天下 提交于 2019-12-06 01:41:34
问题 I am unable to read data in Pandas: Input: import pandas as pd data = 'a,b,c\n1,2,3\n4,5,6' pd.read_csv(StringIO(data),skipinitialspace=True) Output: NameError:name 'StringIO' is not defined Please let me know why the error occurred and also let me know what to import. 回答1: Found the solution here: The error occurred because I didn't import StringIO . Unlike Python 2, in Python 3 you are required to import it. from io import StringIO After importing no error occurred. Output to the above