问题列表 - 第34083页

__repr __()函数的最佳输出类型和编码实践?

最近,我有很多的麻烦__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)

以下是一些困扰我的其他问题,我正在寻找解决它们的解决方案:

  1. 打印到UTF-8终端应该可以工作(我已经sys.stdout.encoding设置了UTF-8,但最好是其他情况也能正常工作).
  2. 将输出管道输出到文件(以UTF-8编码)应该可以工作(在这种情况下,sys.stdout.encodingNone).
  3. 我的许多__repr__()功能的代码目前有很多return ….encode('utf-8'),而且很重要.有什么强劲和轻松的吗?
  4. 在某些情况下,我甚至有丑陋的野兽return ('<{}>'.format(repr(x).decode('utf-8'))).encode('utf-8'),即对象的表示被解码,放入格式化字符串,然后重新编码.我想避免这种错综复杂的转变.

为了编写__repr__()与这些编码问题相关的简单函数,您建议做什么?

python encoding ascii repr

66
推荐指数
2
解决办法
9538
查看次数

是否可以从未保存的EF4上下文中看到添加的实体?

我刚刚进入实体框架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)

entity-framework entity-framework-4

3
推荐指数
1
解决办法
1093
查看次数

()=> C#中的运算符的含义(如果存在)

在Jon Skeet的回答中,我在这里阅读了这条有趣的内容.

有趣的是这一点,他提倡使用代表:

Log.Info("I did something: {0}", () => action.GenerateDescription());
Run Code Online (Sandbox Code Playgroud)

问题是,这是什么()=>运算符,我想知道?我尝试使用谷歌搜索,但由于它是由符号组成的谷歌,实际上并没有多大帮助.我在这里尴尬地错过了什么吗?

.net c# delegates operators

39
推荐指数
2
解决办法
3万
查看次数

ASP.NET MVC数据注释使用继承的RegularExpressionAttribute进行客户端验证

为了保持我的模型验证干净,我想实现我自己的验证属性,比如PhoneNumberAttributeEmailAttribute.其中一些可以有利地实现为继承的简单类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

20
推荐指数
2
解决办法
5327
查看次数

gdata未知授权标头

我正在写一个网络应用程序来自动搜索谷歌的嗡嗡声.

我写了一个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)

c# gdata-api gdata

5
推荐指数
1
解决办法
925
查看次数

ASP.Net MVC DropdownList和选定的值

我能够为我的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)

asp.net asp.net-mvc

1
推荐指数
1
解决办法
9568
查看次数

Spring框架:使用util:map填充Map <Enum,Object>

我有这个工厂类,我想通过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)

java spring

14
推荐指数
2
解决办法
5万
查看次数

用于CRUD的NHaml T4模板?

我想询问是否有人或已经看过NHAMl的T4模板与MVC 2.0的默认T4 CRUD(列表,创建等)模板相同?

c# asp.net-mvc t4 nhaml asp.net-mvc-2

26
推荐指数
1
解决办法
1534
查看次数

在DataGridView列中格式化TimeSpan

我见过这些 问题,但都涉及CellStyle Format值中没有的方法.我只想显示小时和分钟部分(16:05); 不是秒(16:05:13).我试图强制秒值为零,但仍然像16:05:00.如果没有使用像提供字符串或DateTime(并且仅显示小时/分钟部分)的kludge,那么我可以通过任何方式获得格式化以执行我想要的操作.

c# formatting timespan datagridview winforms

7
推荐指数
3
解决办法
1万
查看次数

多个文件中的全局变量

我有两个需要访问公共变量的源文件.做这个的最好方式是什么?例如:

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还是应该在两个文件包含的头文件中?

c++

66
推荐指数
2
解决办法
8万
查看次数