最近,我有很多的麻烦__repr__(),format()和编码. 输出__repr__()应该编码还是unicode字符串?__repr__()Python中 的结果是否有最佳编码?我想输出的内容确实有非ASCII字符.
我使用Python 2.x,并希望编写可以轻松适应Python 3的代码.程序因此使用
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function # The 'Hello' literal represents a Unicode object
Run Code Online (Sandbox Code Playgroud)
以下是一些困扰我的其他问题,我正在寻找解决它们的解决方案:
sys.stdout.encoding设置了UTF-8,但最好是其他情况也能正常工作).sys.stdout.encoding是None).__repr__()功能的代码目前有很多return ….encode('utf-8'),而且很重要.有什么强劲和轻松的吗?return ('<{}>'.format(repr(x).decode('utf-8'))).encode('utf-8'),即对象的表示被解码,放入格式化字符串,然后重新编码.我想避免这种错综复杂的转变.为了编写__repr__()与这些编码问题相关的简单函数,您建议做什么?
我刚刚进入实体框架4,并最终希望使用POCO将其包装在存储库模式中.我发现了一些我没想到的东西.看来,如果您创建上下文,向其添加对象(不保存上下文)并再次查询上下文,则它不会在结果中包含新对象.难道我做错了什么?它似乎应该返回我添加的内容,即使我还没有将结果保存回数据库.这是我的示例代码:
ShopEntities context = new ShopEntities();
// there is only 1 customer so far
var customers = from c in context.Customers
select c;
Console.WriteLine(customers.Count()); // displays 1
Customer newCustomer = context.Customers.CreateObject();
newCustomer.FirstName = "Joe";
newCustomer.LastName = "Smith";
context.Customers.AddObject(newCustomer);
var customers2 = from c in context.Customers
select c;
Console.WriteLine(customers2.Count()); // still only displays 1
context.SaveChanges();
var customers3 = from c in context.Customers
select c;
Console.WriteLine(customers3.Count()); // only after saving does it display 2
Run Code Online (Sandbox Code Playgroud) 在Jon Skeet的回答中,我在这里阅读了这条有趣的内容.
有趣的是这一点,他提倡使用代表:
Log.Info("I did something: {0}", () => action.GenerateDescription());
Run Code Online (Sandbox Code Playgroud)
问题是,这是什么()=>运算符,我想知道?我尝试使用谷歌搜索,但由于它是由符号组成的谷歌,实际上并没有多大帮助.我在这里尴尬地错过了什么吗?
为了保持我的模型验证干净,我想实现我自己的验证属性,比如PhoneNumberAttribute和EmailAttribute.其中一些可以有利地实现为继承的简单类RegularExpressionAttribute.
但是,我注意到这样做会破坏这些属性的客户端验证.我假设有某种类型的绑定在某处失败.
我能做些什么来让客户端验证工作?
代码示例:
public sealed class MailAddressAttribute : RegularExpressionAttribute
{
public MailAddressAttribute()
: base(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$")
{
}
}
Run Code Online (Sandbox Code Playgroud) regex asp.net asp.net-mvc client-side-validation data-annotations
我正在写一个网络应用程序来自动搜索谷歌的嗡嗡声.
我写了一个C#库来管理"Oauth dance"并且它工作正常,我可以得到oauth_token和oauth_token_secret.
我使用www.googlecodesamples.com/oauth_playground/来验证我的oauth_token和oauth_token_secret,它运行正常.我使用GET和https://www.googleapis.com/buzz/v1/activities/@me/@self 对其进行测试以获取用户的流,它可以正常工作.
但现在
我正在尝试使用我的C#库做同样的事情,但我总是得到这个错误:
<?xml version="1.0" encoding="UTF-8"?>
<errors xmlns="http://schemas.google.com/g/2005">
<error>
<domain>GData</domain>
<code>invalid</code>
<location type="header">Authorization</location>
<internalReason>Unknown authorization header</internalReason>
</error>
</errors>
Run Code Online (Sandbox Code Playgroud)
我的请求标题与操场上的标题相同.
Accept: */*
Content-Type: application/atom+xml
Authorization: OAuth oauth_version="1.0", oauth_nonce="9216320",
oauth_timestamp="1283430867", oauth_consumer_key="www.mysite.com",
oauth_token="1%2FZodlNmPP96GT11vYaWA0y6QoqKLqNqZ8bNmxknZZZc",
oauth_signature_method="HMAC-SHA1",
oauth_signature="Tuu82feKNWa4CxoDUyvtIEVODRA%3D"
GData-Version: 2.0
Run Code Online (Sandbox Code Playgroud)
这是C#代码:
string headAuth = "Authorization: OAuth oauth_version=\"1.0\", oauth_nonce=\"9216320\", oauth_timestamp=\"1283430867\",
oauth_consumer_key=\"www.mysite.com\", oauth_token=
\"1%2FZodlNmPP96GT11vYaWA0y6QoqKLqNqZ8bNmxknZZZc\",
oauth_signature_method=\"HMAC-SHA1\", oauth_signature=
\"Tuu82feKNWa4CxoDUyvtIEVODRA%3D\"";
HttpWebRequest req1 =(HttpWebRequest)HttpWebRequest.Create("https://www.googleapis.com/buzz/v1/activities/@me/@self");
req1.Method = "GET";
req1.Accept = "*/*";
req1.ContentType = "application/atom+xml";
req1.Headers.Add("Authorization", headAuth);
req1.Headers.Add("GData-Version", "2.0");
try
{
HttpWebResponse response1 =(HttpWebResponse)req1.GetResponse();
using (var sr = new …Run Code Online (Sandbox Code Playgroud) 我能够为我的MVC项目创建一个下拉列表,但该值未在下拉列表中选择为选定值.它只是从第一个显示整个县列表.值来自数据库.我尝试从以前的帖子中找到它,但它很混乱.请建议我任何想法.
控制器代码
public ActionResult Edit(int i)
{
var items = new SelectList(db.MST_COUNTRies.ToList(), "COUNTRY_ID", "COUNTRY_NM");
ViewData["MST_COUNTRies"] = items;
}
Run Code Online (Sandbox Code Playgroud)
查看代码
<%= Html.DropDownList("COUNTRY_ID", (SelectList)ViewData["MST_COUNTRies"])%>
Run Code Online (Sandbox Code Playgroud) 我有这个工厂类,我想通过spring连接到地图的运行时配置.该映射包含枚举对象和标准pojo.
public class GenericEntityFactoryImpl implements GenericEntityFactory
{
private Map<IndexType,IEntity> indexEntityMap = null;
@Override
public IEntity getIndexEntity(IndexType index) {
return indexEntityMap.get(index);
}
public Map<IndexType, IEntity> getIndexEntityMap() {
return indexEntityMap;
}
public void setIndexEntityMap(Map<IndexType, IEntity> indexEntityMap) {
this.indexEntityMap = indexEntityMap;
}
}
Run Code Online (Sandbox Code Playgroud)
我在使用spring util时出现问题:地图布线,因为在确定键值时我不确定如何正确引用特定的枚举类型.地图值的bean ref很简单.弹簧图布线的所有例子似乎都假设键是一个字符串!
<!-- the value object bean -->
<bean id="cell" class="com.xx.xx.common.index.entity.CellEntity"/>
<bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl">
<util:map
id="indexEntityMap"
map-class="java.util.HashMap"
key-type="com.xx.xx.common.index.IndexType"
value-type="com.xx.xx.common.index.GenericEntityFactoryImpl">
<entry key="CELL">
<ref bean="cell"/>
</entry>
</util:map>
</bean>
Run Code Online (Sandbox Code Playgroud)
编辑
所以我重构了映射
<bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl" >
<property name="indexEntityMap">
<map >
<entry key="com.xx.xx.common.index.CELL"><ref bean="cell"/></entry> …Run Code Online (Sandbox Code Playgroud) 我想询问是否有人或已经看过NHAMl的T4模板与MVC 2.0的默认T4 CRUD(列表,创建等)模板相同?
我有两个需要访问公共变量的源文件.做这个的最好方式是什么?例如:
source1.cpp:
int global;
int function();
int main()
{
global=42;
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
source2.cpp:
int function()
{
if(global==42)
return 42;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
变量global的声明是静态的,extern还是应该在两个文件包含的头文件中?