When I execute, I am getting an error: 64x/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/Kernel_require.rb:54:in 'require': Cannot load such file
require "net/http"
require "uri"
require "nokogiri"
uri = URI.parse("http://www.google.com")
response = Net::HTTP.get_response(uri)
puts parse_body(response.body)
def parse_body(response)
begin
return Nokogiri::XML(response) { |config| config.strict }
rescue Nokogiri::XML::SyntaxError => e
return "caught exception: #{e}"
end
end
Try using require_relative generally when ever you get this issue. (not the best way though!)
Try this
$:.unshift File.join(File.dirname(__FILE__), ".")
Related
I have been using the following code in production for the last couple of months,
#task
def sql_run_procs():
"""This is a delete and update..."""
# Get our logger
logger = prefect.utilities.logging.get_logger() # type: ignore
conn = connect_db(prefect.config.kv.p.prod_db_constring, logger) ## wrapper around create_engine()
with conn.connect() as con:
try:
r = con.execute(
f"EXECUTE fs.spETL_MyProc '{prefect.config.kv.p.staging_db_name}'"
).fetchall()
for q in r[0]:
if q == 1:
logger.info(f"Query {q} has failed")
raise signals.FAIL()
except :
raise SQLAlchemyError("Error in SQL Script")
So like any good coder I copy and pasted the code into another script
#task
def sql_run_procs():
"""This is a clean, truncate and insert"""
# Get our logger
logger = prefect.utilities.logging.get_logger() # type: ignore
conn = connect_db(prefect.config.kv.p.prod_db_constring, logger)
with conn.connect() as con:
try:
r = con.execute(
f"EXECUTE forms.spETL_MyOtherProc '{prefect.config.kv.p.staging_db_name}'"
).fetchall()
for q in r[0]:
if q == 1:
logger.info(f"Query {q} has failed")
raise signals.FAIL()
except :
raise SQLAlchemyError("Error in SQL Script")
And got the following error:
AttributeError: 'NoneType' object has no attribute 'fetchall'
The only difference other than the name of the stored procedure is they're in different Prefect projects. I've searched this site and others for a possible solution but had no success. I know that it's probably something staring me right in the face but after an hour and a half... you know. Thanks in advance.
I am developing a web application that takes a word file and performs tokenization.
I noticed that the document is passed correctly from angularJS to Flask, but there is an error that I can't give an explanation:
Traceback (most recent call last):
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "app.py", line 43, in tokenizer
myDoc = word.Documents.Open(pathToProc, False, False, True) #stackoverflow
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Word.Application.Documents
The document is passed by angularJS with the following code:
var f = document.getElementsByTagName("form")[0].children[1].files[0].name;
if (f != ""){
$http({
url: '/tokenizeDoc',
method: "GET",
params: {doc : f}
});
}
Subsequently it is read by Flask with the following script, and the error falls in the line with the error comment:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import string
import win32com.client
import nltk
import os
from collections import Counter
from pywintypes import com_error
from flask import request, Flask, render_template, jsonify
word = win32com.client.Dispatch("Word.Application")
word.Visible = False
app = Flask(__name__)
#app.route('/')
def landingPage():
return render_template('homepage.html')
#app.route('/tokenizeDoc', methods = ['GET'])
def tokenizer():
if request.method == 'GET':
pathToProc = request.values.get("doc")
sent_tokenizer = nltk.data.load('tokenizers/punkt/italian.pickle')
it_stop_words = nltk.corpus.stopwords.words('italian') + ['\n', '\t', '']
trashes = it_stop_words + list(string.punctuation)
tokensTOT = []
try:
myDoc = word.Documents.Open(pathToProc, False, False, True) #ERROR!!!
sentences = sent_tokenizer.tokenize(word.ActiveDocument.Range().Text)
myDoc.Close()
del myDoc
for sentence in sentences:
tokensTOT = tokensTOT + [t.lower() for t in nltk.word_tokenize(sentence)
if t.lower() not in trashes]
except com_error:
print('IMPOSSIBILE DECIFRARE IL FILE')
return ''
I hope the win32com library is not incompatible with web frameworks and someone can give me an answer.
Many thanks in advance.
use os.path.abspath(pathToProc) instead of pathToProc myDoc = word.Documents.Open(pathToProc, False, False, True) #ERROR!!!
I faced the same problem.
The only solution that I managed to apply is to execute win32com.client.Dispatch individually for each call of the view function.
You also need to execute pythoncom.CoInitialize() for normal working flask multi-threading
import pythoncom
import win32com.client
def get_word_instance():
pythoncom.CoInitialize()
return win32com.client.Dispatch("Word.Application")
#app.route('/tokenizeDoc', methods=['GET'])
def tokenizer():
word = get_word_instance()
word.visible = False
The creation instance of COM-object can be a resource-intensive process. If you need more performance when working with COM objects, then you may need to consider the option of disabling the multithreading of the Flask application.
app.run(threaded=False)
Then you can use your code without any changes
I have a VB.net web service that was written a number of years ago and I "think" might have some issues.
I am not a VB.net guy really and have limited knowledge of Close Dispose protocols. Reading got me the following but I am still unclear.
https://stackoverflow.com/questions/4439409/open-close-sqlconnection-or-keep-open
My main concern is the handling of the MS SQL database, potential lock-ups etc. Have seen a few.
The following code is part of one function (truncated code) but you will see there are a number of 'Exit Function' lines.
I assume then that the 'Finally' code will not get executed and therefor that no Close / Dispose etc. will be execute ? The web service processing returns to the calling app after that Exit Function.
Is that an issue, not processing that 'Finally' chunk of code (Close/Dispose) ?
and If so I guess removing the Exit Function lines will address that ?
Or .. will putting a CloseDbConnection() before the Exit Function do just as well.
thanks
ElseIf AppMode = "Update" Then
InPhoneGUID = db.updateInPhoneScanner(returnedUID, AppMode, New List(Of String)(New String() {tmpl1}))
If Not InPhoneGUID = "" Then
r.Message = "PhoneScanner Templates Updated"
' resultList.Add(r) ' Doubling up on the Returned info ?
r.GenUID = InPhoneGUID
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
Else
r.Message = "error 1,PhoneScanner Update Failed"
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
End If
_Logger.Info("=== Invalid Account Type for PHONESCANNER ===")
r.Message = "error 1,Account Addition Inavild Type"
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
End If
End If ' End =========== MAINLINE ROUTINE
_Logger.Info("=== Invalid MODE ===")
r.Message = "error 1,Inavild Mode Sent"
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
End If
End If
Catch ex As Exception
_Logger.Error(ex)
Finally
db.CloseDbConnection()
If fingerPrintHelper IsNot Nothing Then
fingerPrintHelper.Dispose()
End If
db = Nothing
End Try
The db.CloseConnection is as follows ;
Public Sub CloseDbConnection()
Try
mSqlconnection.Close()
mSqlconnection.Dispose()
mSqlconnection = Nothing
Catch ex As Exception
'Throw New Exception("CloseDbConnection : " & ex.Message)
End Try
End Sub
In a ModelForm I can write a clean_<field_name> member function to automatically validate and clean up data entered by a user, but what can I do about dirty json or csv files (fixtures) during a manage.py loaddata?
Fixtures loaded with loaddata are assumed to contain clean data that doen't need validation (usually as an inverse operation to a prior dumpdata), so the short answer is that loaddata isn't the approach you want if you need to clean your inputs.
However, you probably can use some of the underpinnings of loaddata while implementing your custom data cleaning code--I'm sure you can easily script something using the Django serialization libs to read your existing data files them in and the save the resulting objects normally after the data has been cleaned up.
In case others want to do something similar, I defined a model method to do the cleaning (so it can be called from ModelForms)
MAX_ZIPCODE_DIGITS = 9
MIN_ZIPCODE_DIGITS = 5
def clean_zip_code(self, s=None):
#s = str(s or self.zip_code)
if not s: return None
s = re.sub("\D","",s)
if len(s)>self.MAX_ZIPCODE_DIGITS:
s = s[:self.MAX_ZIPCODE_DIGITS]
if len(s) in (self.MIN_ZIPCODE_DIGITS-1,self.MAX_ZIPCODE_DIGITS-1):
s = '0'+s # FIXME: deal with other intermediate lengths
if len(s)>=self.MAX_ZIPCODE_DIGITS:
s = s[:self.MIN_ZIPCODE_DIGITS]+'-'+s[self.MIN_ZIPCODE_DIGITS:]
return s
Then wrote a standalone python script to clean up my legacy json files using any clean_ methods found among the models.
import os, json
def clean_json(app = 'XYZapp', model='Entity', fields='zip_code', cleaner_prefix='clean_'):
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = app+".settings"
settings = __import__(app+'.settings').settings
models = __import__(app+'.models').models
fpath = os.path.join( settings.SITE_PROJECT_PATH, 'fixtures', model+'.json')
if isinstance(fields,(str,unicode)):
fields = [fields]
Ns = []
for field in fields:
try:
instance = getattr(models,model)()
except AttributeError:
print 'No model named %s could be found'%(model,)
continue
try:
cleaner = getattr(instance, cleaner_prefix+field)
except AttributeError:
print 'No cleaner method named %s.%s could be found'%(model,cleaner_prefix+field)
continue
print 'Cleaning %s using %s.%s...'%(fpath,model,cleaner.__name__)
fin = open(fpath,'r')
if fin:
l = json.load(fin)
before = len(l)
cleans = 0
for i in range(len(l)):
if 'fields' in l[i] and field in l[i]['fields']:
l[i]['fields'][field]=cleaner(l[i]['fields'][field]) # cleaner returns None to delete records
cleans += 1
fin.close()
after = len(l)
assert after>.5*before
Ns += [(before, after,cleans)]
print 'Writing %d/%d (new/old) records after %d cleanups...'%Ns[-1]
with open(fpath,'w') as fout:
fout.write(json.dumps(l,indent=2,sort_keys=True))
return Ns
if __name__ == '__main__':
clean_json()
The waf command waf build shows compiler errors (if there are any) while waf debug or waf release does not and always fails, utilizing the following wscript file (or maybe the wscript file has some other shortcomings I am currently not aware of):
APPNAME = 'waftest'
VERSION = '0.0.1'
def configure(ctx):
ctx.load('compiler_c')
ctx.define('VERSION', VERSION)
ctx.define('GETTEXT_PACKAGE', APPNAME)
ctx.check_cfg(atleast_pkgconfig_version='0.1.1')
ctx.check_cfg(package='glib-2.0', uselib_store='GLIB', args=['--cflags', '--libs'], mandatory=True)
ctx.check_cfg(package='gobject-2.0', uselib_store='GOBJECT', args=['--cflags', '--libs'], mandatory=True)
ctx.check_cfg(package='gtk+-3.0', uselib_store='GTK3', args=['--cflags', '--libs'], mandatory=True)
ctx.check_cfg(package='libxml-2.0', uselib_store='XML', args=['--cflags', '--libs'], mandatory=True)
ctx.check_large_file(mandatory=False)
ctx.check_endianness(mandatory=False)
ctx.check_inline(mandatory=False)
ctx.setenv('debug')
ctx.env.CFLAGS = ['-g', '-Wall']
ctx.define('DEBUG',1)
ctx.setenv('release')
ctx.env.CFLAGS = ['-O2', '-Wall']
ctx.define('RELEASE',1)
def pre(ctx):
print ('Building [[[' + ctx.variant + ']]] ...')
def post(ctx):
print ('Building is complete.')
def build(ctx):
ctx.add_pre_fun(pre)
ctx.add_post_fun(post)
# if not ctx.variant:
# ctx.fatal('Do "waf debug" or "waf release"')
exe = ctx.program(
features = ['c', 'cprogram'],
target = APPNAME+'.bin',
source = ctx.path.ant_glob(['src/*.c']),
includes = ['src/'],
export_includes = ['src/'],
uselib = 'GOBJECT GLIB GTK3 XML'
)
# for item in exe.includes:
# print(item)
from waflib.Build import BuildContext
class release(BuildContext):
cmd = 'release'
variant = 'release'
class debug(BuildContext):
cmd = 'debug'
variant = 'debug'
Error resulting from waf debug :
Build failed
-> task in 'waftest.bin' failed (exit status -1):
{task 46697488: c qqq.c -> qqq.c.1.o}
[useless filepaths]
I had a look at the waf demos, read the wafbook at section 6.2.2 but those did not supply me with valuable information in order to fix this issue.
What's wrong, and how do I fix it?
You need to do at least the following:
def configure(ctx):
...
ctx.setenv('debug')
ctx.load('compiler_c')
...
Since the cfg.setenv function resets whole previous environment. If you want to save previous environment, you can do cfg.setenv('debug', env=cfg.env.derive()).
Also, you don't need to explicitly specify the features = ['c', 'cprogram'], since, it's redundant, when you call bld.program(...).
P.S. Don't forget to reconfigure after modifying wscript file.