Rails,Browserify和与coffeescript的反应(Rails, Browserify and React with coffeescript)
我试图用npm设置React,并因此在没有导轨的情况下使用它 - 反应宝石。 我已经安装了Browserify并作出反应并作出反应。 它似乎有效,我能够运行一个简单的'hello world'示例。“
现在到一些更复杂的东西。 我已经有很多组件加载了rails-react,我需要转换它们。
我已经创建并初始化脚本(init.js.coffee)
React = require('react') ReactDOM = require('react-dom') PhotoRoot = require('photo_root')
和一个组件(photo_root.js.coffee)
module.exports = React.createClass getInitialState: -> null getDefaultProps: -> null showCard: (photoId) -> this.refs.card.setPhotoId photoId render: -> React.DOM.div {className: 'photo-root'}, React.createElement PhotoGrid, showCard: @showCard React.createElement PhotoActionCard, photoId: 615, ref: "card"
这两个文件位于同一个文件夹中。
当我运行这个我从BrowserifyRails :: BrowserifyError得到一个错误:
Error while running `/Users/mh/rails_projects/phototank/node_modules/.bin/browserifyinc -t coffeeify --extension=".js.coffee" --list --cachefile=/Users/mh/rails_projects/phototank/tmp/cache/browserify-rails/browserifyinc-cache.json -o "/Users/mh/rails_projects/phototank/tmp/cache/browserify-rails/output20170205-9947-edkf1y" -`: events.js:154 throw er; // Unhandled 'error' event ^ Error: Cannot find module 'photo_root' from '/Users/mh/rails_projects/phototank/app/assets/javascripts/components' at /Users/mh/rails_projects/phototank/node_modules/browser-resolve/node_modules/resolve/lib/async.js:46:17
它正在寻找'photo_root'在正确的位置,但没有找到它。 它与扩展有关吗? 我正在使用coffeescript!
感谢!
I am trying to setup React with npm, and thus use it without the rails-react gem. I have installed Browserify and react and react-dom. It seems to work, I was able to run a simple 'hello world' example.'
Now to some on the more complex stuff. I already have a lot of components that I have loaded with rails-react and I need to convert these.
I have created and init script (init.js.coffee)
React = require('react') ReactDOM = require('react-dom') PhotoRoot = require('photo_root')
and a component (photo_root.js.coffee)
module.exports = React.createClass getInitialState: -> null getDefaultProps: -> null showCard: (photoId) -> this.refs.card.setPhotoId photoId render: -> React.DOM.div {className: 'photo-root'}, React.createElement PhotoGrid, showCard: @showCard React.createElement PhotoActionCard, photoId: 615, ref: "card"
These two files are in the same folder.
When I run this I get an error from BrowserifyRails::BrowserifyError:
Error while running `/Users/mh/rails_projects/phototank/node_modules/.bin/browserifyinc -t coffeeify --extension=".js.coffee" --list --cachefile=/Users/mh/rails_projects/phototank/tmp/cache/browserify-rails/browserifyinc-cache.json -o "/Users/mh/rails_projects/phototank/tmp/cache/browserify-rails/output20170205-9947-edkf1y" -`: events.js:154 throw er; // Unhandled 'error' event ^ Error: Cannot find module 'photo_root' from '/Users/mh/rails_projects/phototank/app/assets/javascripts/components' at /Users/mh/rails_projects/phototank/node_modules/browser-resolve/node_modules/resolve/lib/async.js:46:17
It is looking for 'photo_root' in the right location, but is not finding it. Does it have something to do with the extensions. I am using coffeescript!!
Appreciated!!
原文:https://stackoverflow.com/questions/42049677
最满意答案
您可以使用正则表达式 ,如下所示:
import re rex = re.compile('([^:]+\S)\s*:\s*(\d+)\s*\(\s*(\d+)/Sec\)') for line in temp: match = rex.match(line) if match: print match.groups()
这会给你:
['Total Requests', '337827', '6687'] ['Total Responses', '337830', '6687'] ['Total Success Connections', '3346', '66'] ['Total Connect Errors', '0', '0'] ['Total Socket Errors', '0', '0'] ['Total I/O Errors', '0', '0'] ['Total 200 OK', '33864', '718'] ['Total 30X Redirect', '0', '0'] ['Total 304 Not Modified', '0', '0'] ['Total 404 Not Found', '303966', '5969'] ['Total 500 Server Error', '0', '0'] ['Total Bad Status', '303966', '5969']
请注意,它只匹配对应于“TITLE:NUMBER(NUMBER / Sec)”的行。 您可以调整表达式以匹配其他行。
You could use a regular expression as follows:
import re rex = re.compile('([^:]+\S)\s*:\s*(\d+)\s*\(\s*(\d+)/Sec\)') for line in temp: match = rex.match(line) if match: print match.groups()
which will give you:
['Total Requests', '337827', '6687'] ['Total Responses', '337830', '6687'] ['Total Success Connections', '3346', '66'] ['Total Connect Errors', '0', '0'] ['Total Socket Errors', '0', '0'] ['Total I/O Errors', '0', '0'] ['Total 200 OK', '33864', '718'] ['Total 30X Redirect', '0', '0'] ['Total 304 Not Modified', '0', '0'] ['Total 404 Not Found', '303966', '5969'] ['Total 500 Server Error', '0', '0'] ['Total Bad Status', '303966', '5969']
Note that will only match lines which correspond to "TITLE:NUMBER(NUMBER/Sec)". You can adapt the expression to match the other lines as well.
相关问答
更多-
您想测试step.followupsteps是否为布尔值true: 'followupsteps': [] if not step.followupsteps else [{ 'id': stepid, } for stepid in step.followupsteps.split(',')] 你是测试,如果not None是真的,它恰好是: >>> bool(not None) True 如果它是一个空字符串, None ,numeric 0或一个空容器,则not step.followup ...
-
如何使用python re.split来分割字符串,但保留数字?(how to use python re.split to split string but keep digits?)[2022-08-03]
你可以使用re.findall : import re s = "100,[+split" new_s = re.findall('\d+|[a-zA-Z\W]', s) 输出: ['100', ',', '[', '+', 's', 'p', 'l', 'i', 't'] You can use re.findall: import re s = "100,[+split" new_s = re.findall('\d+|[a-zA-Z\W]', s) Output: ['100', ',', ... -
"2.7.0_bf4fda703454".split("_")给出了一个字符串列表: In [1]: "2.7.0_bf4fda703454".split("_") Out[1]: ['2.7.0', 'bf4fda703454'] 这会将字符串拆分为每个下划线。 如果要在第一次拆分后停止,请使用"2.7.0_bf4fda703454".split("_", 1) 。 如果你知道这个字符串包含一个下划线,你甚至可以将LHS和RHS分解成单独的变量: In [8]: lhs, rhs = "2.7.0_bf ...
-
string.split('$$TEXT$$') ? >>> a="1.MATCHES$$TEXT$$STRING" >>> a.split("$$TEXT$$") ['1.MATCHES', 'STRING'] >>> a="2.MATCHES $$TEXT$$ STRING" >>> a.split("$$TEXT$$") ['2.MATCHES ', ' STRING'] 和: >>> [x.strip() for x in "2.MATCHES $$TEXT$$ STRING".split("$ ...
-
这是因为Scala中的split与Java中的split相同,后者需要一个正则表达式。 所以| 需要被转义,因为它们代表正则表达式中的一个or运算符。 scala> "string1|~|string2".split("\\|~\\|") res48: Array[String] = Array(string1, string2) That's because split in Scala is the same as split in Java, which expects a regex. So th ...
-
使用re.split()函数和特定的正则表达式模式: import re lst = ["LAKDOER 567-85 06D-1F", "QRT1 35-43 459-70 D", "50201 WSCFVGH 133 9H ", "STREE VDERYG 8C LOP", "GG-STAR THIEOR- WL-515-67-26548H-9" ] pat = re.compile(r'\s(?=[a-z-]*[0-9])', re.I) for s in lst: ...
-
这称为切片 : >>> paragraph[:5], paragraph[5:] ('my pa', 'ragraph') 要回答“拆分每个X字符”问题,您需要一个循环: >>> x = 5 >>> [paragraph[i: i + x] for i in range(0, len(paragraph), x)] ['my pa', 'ragra', 'ph'] 不过有更多解决方案,请参阅: 每个第n个字符拆分python字符串? 在块中迭代列表的最“pythonic”方法是什么? This is c ...
-
拆分字符串点分隔(Split string point delimited)[2022-05-12]
Java split使用正则表达式. 是正则表达式中的特殊字符。 您需要使用字符串"\\."来转义它"\\." 。 Java split uses regular expressions, . is a special character in regular expressions. You need to escape it by using the string "\\.". -
您可以使用正则表达式 ,如下所示: import re rex = re.compile('([^:]+\S)\s*:\s*(\d+)\s*\(\s*(\d+)/Sec\)') for line in temp: match = rex.match(line) if match: print match.groups() 这会给你: ['Total Requests', '337827', '6687'] ['Total Responses', '337830', '668 ...
-
字符串正确分割; ${items[0]}的-e被视为echo的选项。 $ string_val="-e:SQA" $ IFS=: read -a items <<< "$string_val" $ printf '%s\n' "${items[0]}" -e The string is being split correctly; the -e in ${items[0]} is treated as an option to echo. $ string_val="-e:SQA" $ IFS=: rea ...